2) (10 pts)
Let fn be the nth Fibonacci number.
We know f0 = 0, f1 = 1, and when n ≥ 2, fn = fn−1 + fn−2.
The next problem deals with computing fn, given n.
(2a) Given n, suppose we compute fn recursively; it is known that this takes
time exponential in n.
Work through the recursive computation of f (6) and clearly indicate
what you see that suggests recursive computation of f (n) may not be
efficient.
(2b) Give an O(n) time algorithm to compute fn, given n (hint: You can
even output all of f0, f1, · · · , fn, in that order, in O(n) time).
(2c) We want to find a way to compute fn, given n, in O(log n) time. Keep
that in mind as you read through.
Convince yourself of the following matrix multiplication:
[f2,f1] = [1 1, 1 0] [f1, f0]
Which matrix goes on the left hand side of the following:
? = ([1 1, 1 0]^ 2) [f1,f0] = [1 1, 1 0] [1 1, 1 0][f1,f0]
Based on your observations, express [fn+1, fn] in terms of [1 1, 1 0]
and [f1, f0].
Using your expression and ideas from problem (1), briefly describe how
you can compute fn, given n, in O(log n) time. Clearly address the
complexity aspects.