The Tower of Hanoi, Explained

After reading this you will be able to solve the Tower of Hanoi by hand, derive the exact minimum of 2^n - 1 moves, and read the recursion that produces it move by move.

What the puzzle is

You start with a stack of discs on one of three pegs. The discs shrink as they go up, so the largest sits on the bottom and the smallest on top. Your task is to move the whole stack to another peg. Two rules bind you: move one disc at a time, and never place a larger disc on a smaller one.

The puzzle looks trivial with two or three discs and turns punishing fast. Three discs need 7 moves. Ten discs need 1023. The legend that ships with the puzzle claims monks in a temple are moving a 64-disc tower, and that the world ends when they finish. At one move per second that finish is about 585 billion years away, roughly 40 times the current age of the universe. The puzzle is a small machine for feeling exponential growth in your hands.

The hook is that the whole thing reduces to one idea repeated at every scale. To move a stack of n discs, you first move the top n-1 out of the way, move the single biggest disc, then move the n-1 back on top. That sentence is both the solution and the proof of its cost.

When it is worth studying

The Tower of Hanoi earns its place in a first course on recursion because the recursive solution is short, correct, and provably optimal, and because you can watch every step. Reach for it when you want to see three things at once: a problem defined in terms of a smaller copy of itself, a recurrence relation, and a closed-form solution that matches the recurrence exactly.

It is a poor model for almost anything physical. It says nothing about heat, populations, or chaos. Do not read the 64-disc legend as a claim about the real world. It is a way to make the number 2^{64} - 1 \approx 1.845 \times 10^{19} concrete, nothing more. If you want dynamics that do model physical spreading or feedback, the Heat equation simulator and the Logistic map bifurcation are the honest choices.

The three-peg puzzle has exactly one shortest solution for any number of discs, and its length is fixed. There is no clever shortcut below 2^n - 1. That certainty is unusual and is what makes the puzzle a clean teaching object.

The recurrence and its closed form

Let T(n) be the minimum number of moves to shift n discs from one peg to another. The recursive strategy gives a recurrence directly. Moving the top n-1 discs costs T(n-1). Moving the largest disc costs 1. Moving the n-1 discs back costs T(n-1) again.

T(n) = 2\,T(n-1) + 1, \quad T(0) = 0

Here T(0) = 0 because an empty stack needs no moves. Each larger case doubles the previous count and adds one for the single big-disc move. Unrolling the recurrence gives the closed form.

T(n) = 2^n - 1

You can check the algebra by substitution. If T(n-1) = 2^{n-1} - 1, then 2(2^{n-1} - 1) + 1 = 2^n - 2 + 1 = 2^n - 1. The base case holds since 2^0 - 1 = 0, so induction confirms the formula for every n.

The recursion is also a proof of optimality. The biggest disc must move at least once, and before it can move, all n-1 smaller discs must sit on the single spare peg, which itself costs at least T(n-1). After the big disc moves, those n-1 discs must be rebuilt on top, another T(n-1). So no strategy can beat 2\,T(n-1)+1.

Watching the count grow

The numbers double and add one at each step: 1, 3, 7, 15, 31, 63. The table below lists the first several disc counts with the exact minimum and the time at one move per second.

Minimum moves and elapsed time at one move per second
Discs nMoves 2^n - 1Time at 1 move/s
377 seconds
53131 seconds
10102317.05 minutes
20104857512.14 days
324.295e9136.2 years
641.845e19585 billion years

Notice how flat the plot looks until it does not. That is the signature of exponential growth: the curve seems tame for small n and then leaves the page. A log scale straightens it into a line, because \log_2(2^n - 1) \approx n.

Each bar is a little more than double the one before it. By 12 discs the count is 4095, and the first few bars are almost invisible against it.

With 3 discs the minimum is 7 moves and takes 7 seconds at one per second. Each extra disc doubles the total and adds one: 4 discs need 15 moves, 5 need 31, 10 need 1023, 20 need 1048575, and 64 need about 1.845e19 moves, which is roughly 585 billion years at one move per second.

A worked example with three discs

Solving three discs in seven moves

This reproduces the tool's default of three discs. Number the discs 1 (smallest) to 3 (largest). Move the tower from peg A to peg C. The recursion says: move discs 1 and 2 to B, move disc 3 to C, then move discs 1 and 2 onto C.

  1. Disc 1: A to C.
  2. Disc 2: A to B.
  3. Disc 1: C to B. (Discs 1 and 2 are now stacked on B.)
  4. Disc 3: A to C. (The big disc reaches its home.)
  5. Disc 1: B to A.
  6. Disc 2: B to C.
  7. Disc 1: A to C. (Done.)

Seven moves, matching 2^3 - 1 = 7. Steps 1 to 3 move the two-disc substack, which by itself costs 2^2 - 1 = 3. Step 4 is the single big move. Steps 5 to 7 rebuild the substack, another 3. Total: 3 + 1 + 3 = 7.

A second pattern hides in the same list. The smallest disc moves on every odd step (1, 3, 5, 7) and always travels in one direction around the cycle A to C to B to A. Between its moves there is exactly one legal move of another disc. Following those two rules alone solves the puzzle without any recursion at all.

Reading the solution as binary

The move sequence has a clean arithmetic shape. If you number the moves from 1 to 2^n - 1, the disc moved on step m is the position of the lowest set bit in the binary form of m. Move 1 is binary 001, lowest bit in position 1, so disc 1 moves. Move 4 is 100, lowest set bit in position 3, so disc 3 moves. Move 6 is 110, lowest set bit in position 2, so disc 2 moves.

Count how often each disc moves and the pattern falls out. Disc 1 moves on every odd number, so it moves 2^{n-1} times, which is 4 of the 7 moves for three discs. Disc 2 moves 2^{n-2} times (2 moves), and the largest disc moves once. The sum is 2^{n-1} + 2^{n-2} + \dots + 1 = 2^n - 1, the same total.

The smallest disc does half the work. Each larger disc moves half as often as the one below it in size, halving down to a single move for the largest.

Common mistakes

The most frequent error when solving by hand is moving the smallest disc twice in a row. That never helps: two moves of disc 1 could always be replaced by zero or one, so any optimal solution alternates disc 1 with a forced non-trivial move. If you find yourself shuffling the top disc back and forth, you have left the optimal path.

A second mistake is choosing the wrong destination for the substack. To move n discs from A to C, the n-1 stack must go to B, the peg you are not starting from and not ending on. Send it to the wrong peg and you block the big disc.

Do not confuse the three-peg minimum 2^n - 1 with the four-peg version. Adding a fourth peg lowers the minimum sharply (the Frame-Stewart numbers), and the closed form is no longer a clean power of two. Every claim on this page assumes exactly three pegs.

Related tools

If the recursion here interests you, the N-Queens visualizer and the Knight's tour show backtracking, a close cousin that also builds a solution by trying and retreating. The Fifteen puzzle shares the sliding-constraint flavour and adds a parity argument for why half of all positions are unreachable.

For the wider theme of simple rules generating structure, try Langton's ant, Conway's Game of Life, and Collatz orbits. The binary reading of the move sequence connects naturally to Pascal's triangle mod n and to Nim and Grundy numbers, where binary digit sums decide the game.

Frequently asked questions

Why is the minimum exactly 2 to the n minus 1?

Because moving n discs forces you to move the n-1 smaller ones aside, move the largest once, then move them back. That doubles the smaller cost and adds one, and the recurrence T(n) = 2\,T(n-1) + 1 with T(0)=0 solves to 2^n - 1.

How long would 64 discs really take?

At one move per second, 2^{64} - 1 \approx 1.845 \times 10^{19} moves take about 585 billion years, roughly 40 times the 13.8-billion-year age of the universe. Even at a million moves per second it would take about 585 thousand years.

Is there a way to solve it without recursion?

Yes. Move the smallest disc every other turn, always in the same cyclic direction, and on the turns between make the single legal move that does not involve the smallest disc. This produces the identical optimal sequence.

Does the destination peg change the number of moves?

No. Moving from any peg to any other peg needs the same 2^n - 1 moves. The three pegs are symmetric, so only the starting and ending pegs matter, and they cost the same either way.

What changes with more than three pegs?

A fourth peg gives you extra room to park discs, so the minimum drops. For 10 discs the three-peg minimum is 1023, while the four-peg Frame-Stewart minimum is only 49. The clean 2^n - 1 formula applies to three pegs only.