Three pegs stand in a row, and a stack of disks of different sizes sits on the left peg with the largest at the bottom. You may move one top disk at a time and never put a disk on a smaller one. How few moves can carry the whole stack to the right peg? This page builds a solution by recursion, proves with a recurrence relation that no solution for disks is shorter than moves, and shows how quickly that number grows.
Key terms
Tower of Hanoi
Definition The Tower of Hanoi is a puzzle with exactly three pegs and disks of different sizes, where . At the start all the disks are stacked on one peg in order of size, with the largest at the bottom. A move takes the top disk of one peg and puts it on another peg, which must be empty or have a larger disk on top. So only one disk moves at a time, and a disk never rests on a smaller disk. The goal is to move the whole stack to a chosen other peg in as few moves as possible. With three pegs the fewest moves for disks is , and exactly one sequence of that length solves the puzzle. The count belongs to the three-peg puzzle. With a fourth peg some towers can be moved in fewer moves, and the formula no longer applies. The puzzle was published by the French mathematician Édouard Lucas in 1883.
In plain words Before the largest disk can move, every smaller disk has to be out of its way on the spare peg, and after the largest disk reaches the goal they all have to come back on top of it. That is two transfers of a smaller tower plus one move, and it is the whole reason for the count.
Example Name the pegs A, B and C, from left to right, and the three disks small, medium and large. The shortest solution for three disks has seven moves.
| Move | Disk | From | To |
|---|---|---|---|
| 1 | small | A | C |
| 2 | medium | A | B |
| 3 | small | C | B |
| 4 | large | A | C |
| 5 | small | B | A |
| 6 | medium | B | C |
| 7 | small | A | C |
Every move takes a top disk, and no disk ever lands on a smaller one. After move 7 all three disks are on C with the large disk at the bottom. With one disk the puzzle takes one move, and with two disks it takes three, small to the spare peg, large to the goal and small on top of it.
Recursion
Definition Recursion is a way of solving a problem, or of defining a quantity, through smaller cases of the same problem, together with a base case that is handled directly. A recursive method is complete when every chain of smaller and smaller cases ends at a base case. The recursive method for the Tower of Hanoi moves a tower of disks from a starting peg to a goal peg in three stages. It moves the top disks to the third peg by the same method, then moves the largest disk to the goal peg, and then moves the disks from the third peg onto the largest disk by the same method. The base case is a tower of disks, which needs no moves. The largest disk never blocks a smaller one, since any disk may be put on it, so the two smaller transfers are legal.
In plain words You do not plan all the moves at once. To move a big tower you only need to know how to move a tower one disk smaller, and that in turn needs a tower one disk smaller still, until nothing is left to move.
Example For three disks the method moves the two-disk tower from A to B, which takes moves 1 to 3 of the table above, then moves the large disk from A to C in move 4, then moves the two-disk tower from B to C in moves 5 to 7. Both smaller transfers use the two-disk solution of three moves, with the roles of the pegs exchanged. The first goes from A to B with C as the spare peg, and the second goes from B to C with A as the spare peg.
Recurrence relation
Definition A recurrence relation is an equation that gives each term of a sequence in terms of earlier terms. Together with starting values it fixes every term of the sequence. For the three-peg Tower of Hanoi, let be the fewest moves that carry a tower of disks from one peg to another. By symmetry this number is the same for every pair of pegs. It satisfies
and the only sequence with these properties is .
In plain words A recurrence relation tells you how to get the next number from the ones before it. Here each new disk doubles the work of the smaller tower and adds one move for the new largest disk.
Example The recursive method shows that moves are enough. To see that no solution is shorter, take any solution for disks from A to C. Just before the first move of the largest disk, no smaller disk is on top of it and none is on the peg it moves to, so all smaller disks sit together on the third peg. Getting them there took at least moves, because the largest disk never blocks a smaller one, so the moves of the smaller disks form a solution of the puzzle with disks. Just before the last move of the largest disk, which puts it on C for good, the smaller disks again sit together on the peg that is neither its source nor C, and they still need at least moves to reach C. These two stretches of moves do not overlap, and the largest disk moves at least once, so every solution takes at least moves. A solution of exactly that length moves the largest disk only once, and an induction on the number of disks then shows that the recursive solution is the only shortest one. Starting from , the recurrence gives these values.
| Disks | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Fewest moves | 0 | 1 | 3 | 7 | 15 | 31 |
| 1 | 2 | 4 | 8 | 16 | 32 |
The last row is the powers of two, which suggests . The formula holds for , and if it holds for then
so it holds for every .
Exponential growth
Definition A quantity that depends on a whole number grows exponentially when it equals for fixed numbers and , so that each increase of by multiplies it by the same factor . Such a quantity eventually exceeds any quantity of the form , which grows by adding the same amount at each step, and in fact any fixed power of . The fewest moves is one less than the exponential quantity , and each extra disk more than doubles it, since .
In plain words Linear growth adds the same amount at each step, while exponential growth multiplies by the same factor. The tower gets only one disk taller, but the work more than doubles.
Example Ten disks need moves, and twenty disks need moves. Doubling the number of disks from 10 to 20 multiplies the moves by exactly , because
At one move per second, twenty disks take just over 12 days, and thirty disks take seconds, about 34 years. Sixty-four disks take seconds, which is 18,446,744,073,709,551,615 seconds or about 585 billion years.
Check yourself
- Find the fewest moves for six disks. In the recursive solution, how many times does each disk move?
Show the answer
The formula gives moves, and the recurrence agrees, since . Number the disks 1 to 6 from the smallest to the largest. The recursive solution moves disk 6 once, and it moves the five smaller disks with two copies of the five-disk solution. So each smaller disk moves twice as often as it does in the five-disk solution. Repeating this, disk 6 moves once, disk 5 twice, disk 4 four times, disk 3 eight times, disk 2 sixteen times and disk 1 thirty-two times. The check
matches the total. Since the recursive solution is the only shortest one, every shortest solution has these counts.
- Change the rules by adding a fourth peg D, and move three disks from A to D. Find a solution with fewer than seven moves. Which step of the argument for no longer works?
Show the answer
Five moves are enough. Move the small disk from A to B, the medium disk from A to C, the large disk from A to D, the medium disk from C to D and the small disk from B to D. Every move puts a disk on an empty peg or on a larger disk. Five is also the fewest. Before the large disk first moves, the small and medium disks must both have left A, and after it lands on D for good, both must still move onto it, so each of them moves at least twice and the large disk at least once. The argument for three pegs used the fact that, before the largest disk moves, all the smaller disks must sit together on the one remaining peg, which forces a full transfer of the smaller tower. With two spare pegs the smaller disks can wait on different pegs, so that step fails, and for three or more disks the count is no longer the minimum.
- Change the rules another way. Keep three pegs A, B and C in a row, but allow moves only between neighboring pegs, so a disk can never go directly between A and C. Find a recurrence relation and a formula for the fewest moves that carry disks from A to C, and find .
Show the answer
One disk needs two moves, A to B and B to C, so , and . For the largest disk has to go from A to B and later from B to C. Just before its first move, A to B, all smaller disks sit on C, and getting them there from A takes at least moves. Just before its last move, B to C, all smaller disks sit on A. Since they sat on C before, they need at least moves between these two moments. After the last move they need at least moves to get from A to C. These three stretches do not overlap, and the largest disk moves at least twice, so every solution takes at least moves. The method that moves the smaller tower from A to C, the largest disk from A to B, the smaller tower from C to A, the largest disk from B to C and the smaller tower from A to C uses exactly that many. So
Adding one gives , and with this gives . For two disks , which matches . Forbidding one kind of move changes the growth factor from to .