Fibonacci sequence algorithm in Javascript – Developers Writing – Medium

Source: Fibonacci sequence algorithm in Javascript – Developers Writing – Medium

Probably one of the most famous algorithms ever, but still lot of people struggles when trying to find an efficient solution. Let me introduce you to the Fibonacci sequence.

Statement

Given a number N return the index value of the Fibonacci sequence, where the sequence is:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

After a quick look, you can easily notice that the pattern of the sequence is that each value is the sum of the 2 previous values, that means that for N=5 → 2+3 or in maths:

F(n) = F(n-1) + F(n-2)
....