Challenge 3: The nth Fibonacci Number

Problem Statement

Implement a function that takes a variable testVariable and finds the number that is placed at that index in the Fibonacci sequence.

What is the Fibonacci Sequence?

The Fibonacci sequence is one of the most famous sequences 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 … $

Indexing starts at 00. Therefore, at index 00 we have the element 00, at index 11 we have the element 11, at index 22 we have the element 11, and so on.

Generic Mathematical Notation of Fibonacci Sequence

Any number, at index nn in the series, can be calculated using the following equation:

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

By default, the first* and the second number in the sequence are 00 and 11

FF0 =0= 0

FF1 =1= 1

FF2 =F=F0+F+F1 =0+1=1=0 + 1= 1

FF3 =F=F1+F+F2 =1+1=2=1 + 1= 2

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

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

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

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

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

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