Computing Fibonacci Sequence

What is a Fibonacci Sequence?

The Fibonacci sequence is one of the most famous formulas in mathematics. Each number in the sequence is the sum of the two numbers that precede it.

So, the sequence goes:

$ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 … $

Generic Mathematical Notation

Any number, at position n in the series, can be calculated using the following equation:

FFn =F=Fn-2+F+Fn-1

By default, the first and second number in the sequence are 0 and 1

FF1 =0= 0

FF2 =1= 1

FF3 =F=F1+F+F2 =1+0=1=1 + 0= 1

FF4 =F=F2+F+F3 =1+1=2=1 + 1= 2

FF5 =F=F3+F+F4 =1+2=3=1 + 2= 3

FF6 =F=F4+F+F5 =2+3=5=2 + 3= 5

FF7 =F=F5+F+F6 =3+5=8=3 + 5= 8

FF8 =F=F6+F+F7 =5+8=13=5 + 8= 13

Below is a visualization for the computation of the first eight elements in the Fibonacci sequence:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.