
Given a number n, print n-th Fibonacci Number.
Examples:
Input : n = 2 Output : 1 Input : n = 9 Output : 34
The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
Fibonacci series adds two previous numbers for nth Number. Here is the Mathematical Calculation.
Fn = Fn-1 + Fn-2
F0 = 0,
F1 = 1,
F2 = F0 + F1 = 1 + 1 = 2;
F3 = F2 + F1 = 2 + 1 = 3;
Now, We can use same solution into program for the solution.
/**
* 3. Fibonacci series
*/
function fibArr(num) {
let arr = [];
arr[0] = 0;
arr[1] = 1;
for (let i = 2; i < num; i++) {
arr[i] = arr[i-1] + arr[i-2];
}
return arr;
}
console.log(fibArr(2));
console.log(fibArr(9));