T(1)T(2)T(3)T(4)T(5)2¹2²2³2⁴2⁵M(1)M(2)M(4)M(8)M(16)1def T(n):2 if n == 1: return 13 return 2*T(n-1) + 14def M(n):5 if n == 1: return 06 return 2*M(n//2) + n
Value cells: the case on top, its value belowCalculator: the rule being applied right nowPython card: both recurrences as recursive functionsTwo recurrences filled in from their base cases. Towers of Hanoi: T(1) = 1 and T(n) = 2T(n−1) + 1 give 1, 3, 7, 15, 31; adding 1 to each gives the powers of 2, so T(n) = 2ⁿ − 1, proved by induction. Merge sort: M(1) = 0 and M(n) = 2M(n/2) + n give 0, 2, 8, 24, 64 for n = 1, 2, 4, 8, 16, which is n log₂ n. Towers of Hanoi has three pegs and n disks of different sizes, largest at the bottom. É. Lucas published it in 1883. The rule 2T(n−1) + 1 is also the fewest moves possible: before the biggest disk can move, the other n−1 disks must all sit on the third peg.

The recurrence

  1. Towers of Hanoi: move a stack of n disks to another peg, one disk at a time, never putting a big disk on a smaller one. Call the fewest moves T(n). The table will hold T(1) to T(5).

Unroll it

Prove it

Divide and conquer

Try it in the Python prompt

;; step 1/12 · A puzzle that repeats
>>> def T(n):
...     if n == 1: return 1
...     return 2*T(n-1) + 1
...
>>> [T(n) for n in range(1, 6)]
[1, 3, 7, 15, 31]

Checking many values with a computer is evidence, not proof. Induction proves a formula for every n: the base case starts the chain, and the step shows that each case hands the truth to the next.