[DC5] Efficient Fibonacci calculation (3 pts) [00Q3]
Recall the Fibonacci numbers F_n:
As you may know, directly turning the definition of Fibonacci numbers into a recursive implementation is a terrible idea; the resulting algorithm takes \Theta (\varphi ^n) time (where \varphi = (1 + \sqrt 5)/2 \approx 1.618\dots ).
The usual iterative algorithm to repeatedly calculate the next Fibonacci number from the previous two seems like it would take \Theta (n) time to compute F_n: it just loops from 1 to n and does one addition each loop, right? Well... yes, it does one addition each loop, but we can’t really assume that these additions take constant time, because the Fibonacci numbers involved can get quite large! Let’s analyze the situation more carefully.
- Recall from a previous homework problem that the size of the nth Fibonacci number F_n is \Theta (\varphi ^n). Given this fact, approximately how many bits (in terms of \Theta ) are needed to represent F_n? Simplify your answer as much as possible. (Note, you need note have completed GCD4 to do this problem.)
- Now suppose we implement the usual iterative algorithm as follows: initialize an array F of size n, which can hold arbitrary-size integers. Initialize F[0] = 0 and F[1] = 1. Then loop i from 2 to n, and at each iteration, set F[i] \gets F[i-1] + F[i-2]. Taking into account the time needed to add integers of a given size, what is the running time of this algorithm?
- We can do better! In addition to their definition, Fibonacci numbers satisfy the following recurrences (you can just take my word for it, or see this exercise):
\begin {align*}
F_{2n+1} & = F_n^2 + F_{n+1}^2 \\
F_{2n} &= 2F_n F_{n+1} - F_n^2
\end {align*} For example, F_7 = 13 = F_3^2 + F_4^2 = 2^2 + 3^2, and F_8 = 21
= 2F_4F_5 - F_4^2 = 2 \cdot 3 \cdot 5 - 3^2.
Explain how to turn these recurrences into a recursive algorithm for computing Fibonacci numbers, and implement this algorithm in a programming language of your choice. Be careful not to make more recursive calls than necessary!
- Analyze the running time of this algorithm. Be sure to include the time needed to do any additions or multiplications. Assume we will use Karatsuba’s algorithm for multiplication.