Solution Review: Return Sequence of Fibonacci Numbers

This lesson discusses how you can use iterators to return return a Fibonacci sequence.

We'll cover the following

Solution:

The first and second elements of the Fibonacci sequence are 0 and 1 respectively. Each successive element is obtained by adding the two elements before it. For instance, the 3rd element is obtained by adding the 1st and 2nd elements, the 4th is obtained by adding the 2nd and 3rd elements, and so on. Thus we can conclude the following for myArray in a Fibonacci sequence:

myArray[i]=myArray[i2]+myArray[i1]myArray[i] = myArray[i-2] + myArray[i-1]

We used this logic in lines 11 to 14 of the following code to calculate the Fibonacci sequence up to a certain range n.

Get hands-on with 1200+ tech skills courses.