xxxxxxxxxx
function _fib(number) {
if (number === 0 || number === 1) {
return number;
} else {
return _fib(number - 1) + _fib(number - 2)
}
}
xxxxxxxxxx
// 定義一個計算 Fibonacci Sequence 的函式
function fibonacci(n) {
if (n <= 1) {
return n; // 基本情況: 當 n 為 0 或 1 時,返回 n
} else {
// 否則,遞迴計算前兩個數字的和
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
// 創建一個函式來展示 Fibonacci Sequence
function displayFibonacciSequence(n) {
for (let i = 0; i < n; i++) {
console.log(fibonacci(i));
}
}
// 使用示例:顯示前 10 個 Fibonacci 數字
displayFibonacciSequence(10);
xxxxxxxxxx
int fabseq(int nth){
if (nth == 1 || nth == 2){
return 1;
}
int equ = (1/sqrt(5))*pow(((1+sqrt(5))/2), nth)-(1/sqrt(5))*pow((1-sqrt(5))/2, nth);
return equ;
}
xxxxxxxxxx
Language: Rust (not supported by grepper)
fn fibcalculate(x:i32) -> i32 {
let mut two_nums_ago = 0;
let mut current_num = 1;
let mut last_num = 0;
for i in 1..x { // or 0..x if you start fibonacci from 0, although that breaks the math law as 0+0 != 1
last_num = current_num;
current_num += two_nums_ago;
two_nums_ago = last_num;
}
return current_num;
}
xxxxxxxxxx
function myFib(n) {
if (isNaN(n) || Math.floor(n) !== n)
return "Not an integer value!";
if (n === 0 || n === 1)
return 3;
else
return myFib(n - 1) + myFib(n - 2);
}
console.log(myFib(5));
xxxxxxxxxx
nterms = int(input())
x, y, z = 1, 1, 0
if nterms == 1:
print(x)
else:
while z < nterms:
print(x, end=" ")
nth = x + y
x = y
y = nth
z += 1