Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fibonacci sequence in Javascript

I am very new to programming in general and am having a hard time understanding this Fibonacci sequence example:

var fib = [0, 1];
for (var i = 2; i < n; i++) {
    fib[ i ] = fib[ i - 1 ] + fib[ i - 2 ];
    console.log(fib);
}

On the first iteration, index 2 is equal to 1, simple enough. But, when I try the second iteration with i = 3, I get:

fib[ 3 ] = fib[ 3 - 1 ] + fib[ 3 - 2 ];  
fib[ 3 ] = fib[ 2 ] + fib[ 1 ]; 
fib[ 3 ] = fib[ 3 ];

Where am I going wrong with my thinking? So far I have:

var fib = [0,1,1,3]

which I know is not correct.

like image 806
KMcA Avatar asked Sep 27 '12 23:09

KMcA


People also ask

How do you make a Fibonacci sequence in JavaScript?

Let's consider an example to get the sum of the Fibonacci series in JavaScript using function and for loop. var n1 = 0; // declaration of variables n1, n2, i and temp. temp = n1 + n2; // store the sum of n1 and n2 in temp variable.

What is Fibonacci series in JavaScript?

Fibonacci series is the series of numbers that are calculated by adding the values of the previous two numbers in the sequence. The first two numbers of the sequence are zero and one.

How do you find the nth Fibonacci number in JavaScript?

The simplest answer is to do it recursively. This has a O(2^n) time complexity but if you memoize the function, this comes down to O(n). // Recursive, O(2^n) const fib = (n) => n < 2 ? n : fib(n-1) + fib(n-2); } // Memoize O(n) // use underscore _.

What is Fibonacci sequence formula?

What is Fibonacci Sequence Formula in Math? The Fibonacci sequence formula deals with the Fibonacci sequence, finding its missing terms. The Fibonacci formula is given as, Fn = Fn-1 + Fn-2, where n > 1.


3 Answers

When you are reasoning about the code, you make the jump from fib[3] = fib[2] + fib[1] to fib[3] = fib[3]. This happens to be a transformation that results in a correct statement, but it is not how it works. This code is adding the value at index 2 to the value at index 1. That is not the same as taking the value at index 3. The way this reasoning should work is as follows:

You start with fib = [0, 1]. Then in the first iteration of the loop you have fib[2] = fib[1] + fib[0]. This means that you add the value at index 0 (which happens to be 0) to the value at index 1 (which happens to be 1) to get the value that you put at the end of the array (1). Then in the second iteration, you do a similar thing, adding the value at index 1 (still 1) to the value at index 2 (also 1) to get 2, which goes at the end of the array. This continues, and at each iteration you add together the last two values in the array to get the next value.

In JavaScript, when using an array like fib, fib[i] refers to the ith value in this array, counting from 0. So fib[0] is the first element in the array, fib[1] is the second element in the array, and so on.

like image 115
murgatroid99 Avatar answered Nov 10 '22 18:11

murgatroid99


fib[ 3 ] = fib[ 3 - 1 ] + fib[ 3 - 2 ];  
fib[ 3 ] = fib[ 2 ] + fib[ 1 ]; 
fib[ 3 ] = fib[ 3 ];

You are adding the indexes up not the value in the array the index points to

fib[ 3 ] = fib[ 3 - 1 ] + fib[ 3 - 2 ];  
fib[ 3 ] = fib[ 2 ] + fib[ 1 ]; 
fib[ 3 ] = 1 + 1;

[0,1,1,2]

fib[0] = 0
fib[1] = 1
fib[2] = 1
fib[3] will equal 2

So next iteration

fib[4] = fib[4-1] +fib[4-2]
fib[4] = fib[3] + fib[2]
fib[4] = 1 + 2
fib[4] = 3
like image 34
Tony Hopkinson Avatar answered Nov 10 '22 17:11

Tony Hopkinson


You're code is fine. Ran this and got the proper output:

var fib = [0, 1];
for (var i = 2; i < 10; i++) {
    fib[ i ] = fib[ i - 1 ] + fib[ i - 2 ];
    console.log(fib);
}

Console: 0,1,1,2,3,5,8,13,21,34

like image 29
Domenic D. Avatar answered Nov 10 '22 17:11

Domenic D.