Skip to content

Runtime blow-up of recursive Fibonacci

Question

A plain recursive routine for the Fibonacci numbers, F(n)=F(n−1)+F(n−2)F(n)=F(n-1)+F(n-2), needs 100100 seconds to compute F(n)F(n) for some large nn. Roughly how long will it take for F(n+1)F(n+1), and how could you compute Fibonacci numbers faster?

Short answer

About 162162 seconds. The run time T(n)=T(n−1)+T(n−2)+cT(n)=T(n-1)+T(n-2)+c grows like F(n)F(n), i.e. by the golden ratio φ≈1.618\varphi\approx1.618 per step, so 100φ≈162100\varphi\approx162 s. Iterating from the bottom up (keeping the last two values) takes O(n)O(n), and matrix powering or fast doubling takes O(log⁡n)O(\log n) operations.

Adapted from Zhou, A Practical Guide to Quantitative Finance Interviews, sec. 7.1 Algorithms, 'Fibonacci numbers', p.178; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question