Sub topic 8
Fibonacci and the Cost of Repeated Work
This chapter assumes you can read Python functions, follow a recursion tree, and use Big-O notation. It shows a recursive function that is correct and short but far too slow to use for large inputs.
The Definition Translates Directly
The Fibonacci sequence starts 0, 1, 1, 2, 3, 5, 8, 13, and so on. Each term is the sum of the two terms before it:
- Base cases: F(0) = 0 and F(1) = 1
- Recursive case: F(n) = F(n-1) + F(n-2) for n ≥ 2
The Python version reads like the math:
def fib(n):
if n < 2:
return n
return fib(n - 1) + fib(n - 2)
print(fib(10)) # 55The function returns correct values. The trouble is its speed. Factorial made one recursive call per level. fib makes two, and those two calls overlap heavily.
Where the Work Repeats
Here is the recursion tree for fib(4):
The left subtree of fib(4) already computes fib(2). The right subtree then computes it again from scratch, because the function keeps no record of earlier answers. In larger trees, whole subtrees repeat: fib(n-2) is computed once directly and once more inside fib(n-1).
Counting the Calls
You can measure the waste by adding a counter:
calls = 0
def fib_counted(n):
global calls
calls += 1
if n < 2:
return n
return fib_counted(n - 1) + fib_counted(n - 2)
for n in (5, 10, 20, 30):
calls = 0
result = fib_counted(n)
print(n, result, calls)Output:
5 5 15
10 55 177
20 6765 21891
30 832040 2692537The call count C(n) follows nearly the same recurrence as Fibonacci: C(n) = C(n-1) + C(n-2) + 1, with C(0) = C(1) = 1. It solves to C(n) = 2·F(n+1) − 1. Because F(n) grows like φⁿ, where φ ≈ 1.618 is the golden ratio, the running time is exponential, roughly O(1.618ⁿ). Each increase of n by 1 multiplies the work by about 1.6. fib(40) makes over 331 million calls.
| n | F(n) | Calls to `fib` |
|---|---|---|
| 5 | 5 | 15 |
| 10 | 55 | 177 |
| 20 | 6,765 | 21,891 |
| 30 | 832,040 | 2,692,537 |
| 40 | 102,334,155 | 331,160,281 |
Only n + 1 distinct values exist, from fib(0) to fib(n). Nearly all of those calls recompute values the program has already found.
Common Errors
- Missing a base case. If you write only
if n == 1: return 1, thenfib(2)callsfib(0), which callsfib(-1), and the recursion continues until it hitsRecursionError. The testn < 2covers both base cases. - Negative input. With
n < 2,fib(-3)returns -3 instead of failing. If negative input is invalid, raiseValueErrorthe way the factorial chapter did. - Assuming short code means fast code. The recursive definition is a specification. It does not tell you how efficient the function will be.
The next chapter keeps the recursive structure and removes the repetition by caching results.
Exercise
When computing fib(6) with the naive function, how many times is fib(2) called? Use the recursion tree pattern to reason, then check with a counter.
Show answer
5 times. In general, fib(k) for k ≥ 1 is called F(n−k+1) times while computing fib(n). For n = 6 and k = 2 that is F(5) = 5.
Exercise
Using C(n) = 2·F(n+1) − 1, how many calls does fib(25) make? F(26) = 121393.
Show answer
2 × 121393 − 1 = 242785 calls.