Exploring an Iterative Algorithm
00:00 Exploring an Iterative Algorithm.
00:04 What if you don’t even have to call the recursive Fibonacci function at all? You can actually use an iterative algorithm to compute the number at position N in the Fibonacci sequence.
00:15 You know that the first two numbers of the sequence are zero and one and that each subsequent number in the sequence is the sum of its previous two predecessors.
00:24 So you can just create a loop that adds the previous two numbers, N minus 1 and N minus 2, together to find the number of position N in the sequence.
00:33 The bold number in the diagram on-screen represents the new numbers that need to be calculated and added to the cache in each iterative step. To calculate the Fibonacci number at position N, you store the first two numbers of the sequence, zero and one, in cache, then calculate the next numbers consecutively until you can return cache N.
00:55 In the next section, you’ll take a look at generating the Fibonacci sequence in Python using different algorithms and programming strategies.
Become a Member to join the conversation.