Discrete Mathematics and Combinatorics

The Tower of Hanoi and the Mathematics of Recursion

Solve the classic disk puzzle, prove that 2ⁿ − 1 moves is the best possible and find binary numbers and fractals hidden inside it.

Key takeaways

  • The minimum number of moves for n disks on three pegs is 2ⁿ − 1.
  • The recursive solution reduces a problem with n disks to two problems with n − 1 disks and one single move.
  • Showing that the method works and that nothing shorter exists are separate arguments, and both are needed.
  • The sequence of moves follows the pattern of binary counting.

The Tower of Hanoi with n disks can be solved in 2ⁿ − 1 moves, and no method does it in fewer. The solution is recursive: move the top n − 1 disks out of the way, move the largest disk to its destination, then move the n − 1 disks back on top of it.

The puzzle has three pegs and a stack of disks of different sizes on one of them, largest at the bottom. You must move the whole stack to another peg, one disk at a time, never placing a larger disk on a smaller one.

Start small

With 1 disk, you move it: 1 move. With 2 disks, put the small one on the spare peg, move the large one to the target, then bring the small one over: 3 moves.

With 3 disks, label the pegs A (start), B (spare) and C (target), and the disks 1 (smallest) to 3. The seven moves are:

1. disk 1:  A → C
2. disk 2:  A → B
3. disk 1:  C → B
4. disk 3:  A → C
5. disk 1:  B → A
6. disk 2:  B → C
7. disk 1:  A → C

Look at the structure. Moves 1 to 3 carry a two-disk tower to B. Move 4 shifts the largest disk. Moves 5 to 7 carry the two-disk tower from B to C.

The recursive idea

To move n disks from a source to a target, you cannot touch the largest disk until all the others are stacked on the third peg. That observation writes the algorithm:

  1. Move n − 1 disks from the source to the spare, using the target for help.
  2. Move the largest disk from the source to the target.
  3. Move n − 1 disks from the spare to the target, using the source for help.

Steps 1 and 3 are the same puzzle with one disk fewer. The largest disk never gets in the way during them, since any disk may sit on it. The process bottoms out at 0 disks, where there is nothing to do. This stopping point is called the base case, and every recursion needs one.

The three-part recursive solution
1 Clear the way Move n − 1 disks to the spare peg 2 Move the giant Largest disk goes to the target 3 Rebuild on top Move n − 1 disks onto the target Clear the way Move n − 1 disks to the spare peg 1 Move the giant Largest disk goes to the target 2 Rebuild on top Move n − 1 disks onto the target 3

Counting the moves

Let T(n) be the number of moves this method makes. From the three steps,

T(0) = 0
T(n) = 2·T(n − 1) + 1

T(1) = 1,  T(2) = 3,  T(3) = 7,  T(4) = 15,  T(5) = 31

The pattern suggests T(n) = 2ⁿ − 1. Prove it by induction: it holds for n = 0, and if it holds for n − 1, then T(n) = 2(2ⁿ⁻¹ − 1) + 1 = 2ⁿ − 1.

Another way to solve it is to add 1 to both sides: T(n) + 1 = 2(T(n − 1) + 1). So T(n) + 1 doubles each time, starting from 1. This is the fixed-point technique for first-order recurrences.

How often each disk moves

Number the disks from 1, the smallest, to n. In the recursive solution the largest disk moves once. Disk n − 1 moves twice, once inside each of the two smaller subproblems. Disk n − 2 moves four times, and so on.

In general disk k moves 2ⁿ⁻ᵏ times, and the smallest disk moves 2ⁿ⁻¹ times, which is more than half of all moves. Adding up gives 1 + 2 + 4 + ... + 2ⁿ⁻¹ = 2ⁿ − 1, a second derivation of the total.

n = 4:   disk 4:  1 move
         disk 3:  2 moves
         disk 2:  4 moves
         disk 1:  8 moves
         total   15

Each disk also keeps to a single direction around the pegs, and neighboring sizes turn in opposite directions. In the three-disk solution, disk 1 visits A, C, B, A, C in turn, while disk 2 goes A, B, C.

Why nothing shorter exists

Showing that one method takes 2ⁿ − 1 moves does not show that it is the best. Here is the missing half.

Any solution must at some point move the largest disk for the first time. At that moment the largest disk is alone on its peg and its destination is empty, so all n − 1 other disks are on the remaining peg. Getting them there takes at least M(n − 1) moves, where M is the true minimum.

After the last time the largest disk moves, the other n − 1 disks must travel from a single peg onto it, which again takes at least M(n − 1) moves. Together with at least one move of the large disk,

M(n) ≥ 2·M(n − 1) + 1

So M(n) ≥ 2ⁿ − 1 by induction, and the recursive method achieves it.

A recipe without recursion

You can solve the puzzle by hand with a simple rule. On every odd-numbered move, move the smallest disk one step around a fixed cycle of pegs. On every even-numbered move, make the only legal move that does not involve the smallest disk.

The direction of the cycle depends on the number of disks. If n is odd, the cycle is source → target → spare. If n is even, it is source → spare → target. Check with the three-disk list above: disk 1 goes A → C, then C → B, then B → A.

Binary numbers in disguise

Write the move numbers 1 to 7 in binary: 1, 10, 11, 100, 101, 110, 111. Count the zeros at the right end of each and add 1. You get 1, 2, 1, 3, 1, 2, 1, which is exactly the list of disks moved.

This happens because counting in binary is recursive in the same way. To count to 2ⁿ − 1, you count through the lower n − 1 bits, switch on the highest bit once, then count through the lower bits again. See binary numbers.

The same pattern lists all n-bit strings so that neighbors differ in a single bit, an ordering called a Gray code. Flip bit number k exactly when the puzzle moves disk k.

Jumping to any moment

You can find the position after any number of moves without playing them. The largest disk moves exactly at move number 2ⁿ⁻¹, the middle of the solution.

So compare your move number m with that midpoint. If m is smaller, the largest disk is still on the source, and you are m moves into the task of shifting n − 1 disks to the spare. Otherwise it is on the target, and you are m − 2ⁿ⁻¹ moves into shifting the n − 1 disks from the spare to the target. Repeat the question for the next disk.

For three disks after 5 moves: 5 is at least 4, so disk 3 is on C, and we are 1 move into moving two disks from B to C. Then 1 is less than 2, so disk 2 is still on B, and we are 1 move into moving disk 1 from B to A. Disk 1 is therefore on A. This matches the list above.

The work needed grows only with n, although the number of moves grows like 2ⁿ.

The map of all positions

In a legal position, each disk sits on one of 3 pegs, and once you know which disks share a peg their order is forced. So there are 3ⁿ legal positions.

Draw a point for each position and join two points when one move connects them. For 1 disk you get a triangle. For n disks you get three copies of the drawing for n − 1 disks, one for each peg of the largest disk, joined by three single edges.

The picture resembles the Sierpiński triangle, a fractal made of ever smaller triangles. The shortest solution runs straight along one side of the large triangle. Pictures like this belong to graph theory.

History, legend and a very large number

The French mathematician Édouard Lucas introduced the puzzle in the 1880s. It was sold with a story about priests moving 64 golden disks, with the world ending when they finished. The tale is best read as part of the puzzle's packaging and not as an ancient tradition.

The arithmetic is still striking. Moving 64 disks takes 2⁶⁴ − 1 = 18,446,744,073,709,551,615 moves. At one move per second without rest, that is roughly 585 billion years, many times the current age of the universe, which is about 13.8 billion years.

Each added disk doubles the work, plus one. This is what exponential growth means, and why exponential running times matter so much in computing.

Variations

Small changes to the rules give new recurrences.

Variant Minimum moves for n disks
Standard three pegs 2ⁿ − 1
Only moves to or from the middle peg, end peg to end peg 3ⁿ − 1
Two disks of each size, equal sizes not distinguished 2ⁿ⁺¹ − 2

With four pegs the problem is far harder. A strategy proposed by J. S. Frame and B. M. Stewart in 1941 moves some small disks to one spare peg, moves the rest using three pegs, then brings the small ones back, choosing the split to minimize the total. It was long conjectured to be optimal, and Thierry Bousch published a proof for four pegs in 2014. For five or more pegs the question has remained open as of the mid-2020s.

What the puzzle teaches

Recursive thinking asks you to trust that the smaller problem can be solved and to concentrate on how to use that solution. You do not need to trace all 1,023 moves for 10 disks to be sure the method works.

A computer program that follows the three steps is only a few lines long. While it runs, it needs to remember at most about n unfinished tasks at once, one for each level of the recursion, even though it will eventually output 2ⁿ − 1 moves. Memory and running time are therefore very different costs here.

Check three things in any recursion: there is a base case, each call is on a strictly smaller problem, and the pieces combine correctly. Those are the same three things an induction proof requires.

Written for 4Mathematics with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 45 articles in Discrete Mathematics and Combinatorics →

Latest news on this topic