Processing math: 100%

Fibonacci Numbers solved by 7229

Feb. 21, 2014, 3:28 p.m. by Rosalind Team

Fibonacci numbersclick to expand

The Fibonacci numbers grow almost as fast as the powers of 2: for example, F30 is over a million, and F100 is already 21 digits long! In general, Fn20.694n. No other sequence of numbers has been studied as extensively, or applied to more fields: biology, demography, art, architecture, music, to name just a few. And, together with the powers of 2, it is computer science’s favorite sequence.

Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.

Problem

The Fibonacci numbers 0,1,1,2,3,5,8,13,21,34, are generated by the following simple rule Fn={Fn1+Fn2,n>1,1,n=1,0,n=0.

Given: A positive integer n25.

Return: The value of Fn.

Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.

Sample Dataset

6

Sample Output

8

Discussionclick to expand

Figure 1. A tree showing the branching, exponential nature of recursive calls in fib1. The bottom row of the tree has calls; once the row grows to depth 20, there are 1,048,576 recursive calls.

One idea is to slavishly implement the recursive definition of Fn. Here is the pseudocode of the resulting algorithm:

Let T(n) be the number of steps needed to compute fib1(n); what can we say about this function? For starters, if n2, then the procedure halts almost immediately, after just a couple of steps. Therefore, T(n)2 for n1. For larger values of n, there are two recursive calls of fib1, taking respective times T(n1) and T(n2), plus three computer steps (checks on the value of n and a final addition). Therefore, T(n)=T(n1)+T(n2)+3 for n>1. Compare this to the recurrence relation for Fn: we immediately see that T(n)Fn.

This is very bad news: the running time of the algorithm grows as fast as the Fibonacci numbers themselves! T(n) is exponential in n, which implies that the algorithm is impractically slow except for very small values of n.

Let’s try to understand why fib1 is so slow. Figure 1 shows the cascade of recursive invocations triggered by a single call to fib1(n). Notice that many computations are repeated! A more sensible scheme would store the intermediate results—the values F0,F1,,Fn1—as soon as they become known.

As with fib1, the correctness of this algorithm is self-evident because it directly uses the definition of Fn. How long does it take? The inner loop consists of a single computer step and is executed n1 times. Therefore the number of computer steps used by fib2 is linear in n. From exponential we are down to polynomial, a huge breakthrough in running time. It is now perfectly reasonable to compute F200 or even F200,000.

Source: Algorithms by Dasgupta, Papadimitriou, Vazirani. McGraw-Hill. 2006.

Visualization

To better feel the difference between polynomial and exponential time, try to compute F20 by using the following visualization by David Galles: http://www.cs.usfca.edu/~galles/visualization/DPFib.html. Selecting "Fibonacci Recursive" shows the enormous number of recursive calls that the program tries to make in fib1. Stop the animation when you get tired of watching it, and instead click "Fibonacci Table" to see just how much quicker using fib2 is. (Note however that a slightly different definition is used there: F0=F1=1.)

Please login to solve this problem.

Welcome to Rosalind!

Rosalind is a platform for learning bioinformatics through problem solving.
Please login with Google/Twitter/Facebook or register a new account.