PlayNook

Math & Logic · Recursion

Towers of Hanoi

The puzzle where the answer is known before you start: 2ⁿ−1 moves, never fewer. The question is whether you can find them — and the counter says the instant you cannot.

There is no strategy to discover here and no opponent to outwit. The shortest solution for n discs is 2ⁿ−1 moves, it has been provable since the 1880s, and you can look it up in a second.

That is exactly what makes it worth playing. The number on the counter is not a score to beat — it is a statement about you. Seven discs take 127 moves. If you take 140, the puzzle has told you something precise about how well you are holding a plan in your head.

How to play Towers of Hanoi

Move the whole tower from A to C. One disc at a time, and never a larger disc onto a smaller one. Click a peg to lift its top disc, click another to put it down.

That is the entire rule set, and it has not changed since 1883.

The counter above the board does the interesting part. It knows the shortest route from wherever you are — not from the start, from here — so the moment you make a move that cannot be part of a shortest solution, it stops saying still optimal and starts counting what you have thrown away. You do not have to finish to find out that you went wrong.

What is the minimum number of moves?

The proof fits in three sentences, and it is the reason this puzzle is on every first-year computing course.

The largest disc has to move at some point. Before it can, all n−1 smaller discs must be somewhere else, and there is only one other peg they can all be on — so they must first be stacked, in order, on the spare peg. After the big disc moves, that stack has to be rebuilt on top of it.

The seven positions of a three-disc solution in a row, with the first three moves marked as one job, the fourth move alone, and the last three as a third job
Three discs, all seven moves, generated by the solver on this page. The middle move is the only one that matters; everything either side of it is the same puzzle one size smaller.

So solving n discs costs twice the price of solving n−1 plus one move for the big disc:

2 × (2ⁿ⁻¹ − 1) + 1 = 2ⁿ − 1

One disc takes one move. Everything else follows. And because each of those steps was forced — there was no choice about stacking the small discs on the spare peg — the count is not just achievable but minimal.

Bar chart of the shortest solution from three discs at seven moves up to ten discs at 1,023 moves, with the 64-disc figure of over eighteen quintillion below
Every figure computed rather than quoted. Ten discs is 1,023 moves — about seventeen minutes at a move a second.

How to solve Towers of Hanoi step by step

There is a way to solve it without planning anything, and we checked it against the solver for every size on this page:

  • On every odd-numbered move — the 1st, 3rd, 5th and so on — move the smallest disc, always in the same direction round the pegs.
  • On every even-numbered move there is exactly one legal move that does not touch the smallest disc. Make it.

The direction depends on the parity of the disc count. With an odd number of discs the small one cycles A → C → B; with an even number, A → B → C. Follow that blindly and you arrive at 2ⁿ−1 without ever holding a plan.

Which is the second thing this puzzle teaches: a problem that looks like it needs recursion can often be run as a loop, and the loop is the one a machine would rather have.

Towers of Hanoi and the Sierpiński triangle

This is the part that turns a first-year exercise into something worth staring at.

Take every position the puzzle can be in — each disc is on one of three pegs, so there are 3ⁿ of them, and all of them are reachable. Draw each as a dot. Join two dots whenever one legal move takes you between them.

All 729 positions of a six-disc tower drawn as dots joined by legal moves, forming a Sierpiński triangle, with the 63-move shortest solution highlighted
Six discs — 729 positions, 1,092 moves between them, plotted from the real positions rather than drawn as a fractal. The three corners are the positions with every disc on one peg.

Nobody arranged that. The biggest disc decides which third of the triangle you are in, the next disc decides which third of that third, and so on down — which is precisely the recipe for constructing a Sierpiński triangle.

The state graphs for one, two, three and four discs side by side, each one three copies of the previous joined by three edges
Three positions, nine, twenty-seven, eighty-one. Each picture is three copies of the one before, joined by the three moves of the new largest disc.

The highlighted line in the big picture is the shortest solution, and its shape is the recursion again. It crosses 1 + 2 + 4 + 8 + 16 + 32 = 63 edges — one long jump, then two of half that length, then four, and so on. It also never once passes through the third corner, which is the position with every disc on the middle peg: the fastest route past a place you never visit.

How long would 64 discs take?

The story arrived with the puzzle. In a temple, priests are moving 64 golden discs between three diamond needles under the same rule, and when they finish, the world ends.

They will finish after 18,446,744,073,709,551,615 moves. At one move per second that is 584 billion years, against a universe currently about 13.8 billion years old. There is no urgency.

The number is the same one that appears in the wheat-and-chessboard story, for the same reason: doubling sixty-four times is a bigger number than people are built to imagine, and both stories exist to make that felt rather than stated.

Who invented Towers of Hanoi?

Édouard Lucas invented the puzzle in 1883 and sold it as a toy under the pseudonym N. Claus de Siam — an anagram of Lucas d'Amiens, and a small joke about a mathematician from Amiens pretending to be a mandarin. He is better remembered for the Lucas numbers and for the primality test named after him, which held the record for finding large primes for most of the twentieth century.

The four-peg version is a useful reminder of how lucky the three-peg case is. Frame and Stewart conjectured a formula in 1941; it was not proved optimal for four pegs until Thierry Bousch did it in 2014. For five pegs and beyond, nobody knows. The clean answer you get here is the exception.

What to try on this board

Start at four discs and get to 15 without the counter turning red. Then move up until you cannot. Most people hold it together to six or seven and come apart somewhere around eight, which is 255 moves and about the point where you have to stop tracking discs and start trusting the pattern.

Then press Explain each move and let it name the recursion step behind every single move, or set the solver going slowly and simply watch. The sub-tower shuttling back and forth is the same shape as the picture above, and once you have seen the connection it is quite hard to unsee.

Frequently asked questions

+ What is the minimum number of moves for Towers of Hanoi?

2ⁿ−1, where n is the number of discs. Three discs take 7 moves, four take 15, ten take 1,023. It is not an estimate or a best-known result: it is provably the minimum, and it is always achievable. That is unusual for a puzzle — you know the answer before you start, and the only question is whether you can find it.

+ Why is it 2ⁿ−1?

Because the largest disc has to move at least once, and before it can, every smaller disc must be stacked out of the way on the third peg. That costs the same as solving the whole puzzle one size smaller. Then the big disc moves, then you rebuild the small stack on top of it — one size smaller again. So the cost doubles and adds one at every step: 2 × (2ⁿ⁻¹ − 1) + 1 = 2ⁿ − 1.

+ How do you solve it without thinking?

There is a recipe, and we checked it holds for every size we could enumerate. Move the smallest disc on every odd-numbered move — 1st, 3rd, 5th and so on — always in the same direction round the three pegs. On every even-numbered move there is exactly one legal move that does not touch the smallest disc, so make that one. The direction depends on parity: with an odd number of discs the small one cycles A → C → B, with an even number A → B → C. Follow that and you land on 2ⁿ−1 without planning anything.

+ How many positions are there?

3ⁿ, and every single one is reachable — 729 for six discs, 59,049 for ten. Each disc is on one of three pegs, and because a stack's order is forced by the rules, that is the whole state. We verified reachability by search: nothing is stranded.

+ What has Towers of Hanoi got to do with the Sierpiński triangle?

Draw every position as a dot and join two dots whenever a single legal move connects them. The picture that comes out is a Sierpiński triangle, exactly — three copies of the smaller picture joined by three edges, repeated. The three corners are the positions with every disc on one peg. It is not an analogy or a resemblance; the graph is that fractal, and the diagram on this page is drawn from the real positions rather than sketched.

+ How long would the 64-disc temple take?

18,446,744,073,709,551,615 moves. At one move a second that is 584,542,046,090 years — around 584 billion, against a universe of about 13.8 billion. The legend of Brahmin priests moving 64 golden discs came with the puzzle in the 1880s and is generally credited to Lucas himself. It is a very good way of saying 2⁶⁴.

+ Who invented it?

Édouard Lucas, a French mathematician, in 1883. He sold it as a toy under the anagram-pseudonym “N. Claus de Siam”, a rearrangement of “Lucas d'Amiens”. He is better known in mathematics for the Lucas numbers and for the primality test that carries his name, which was used to verify record primes for most of the twentieth century.

+ What if there were four pegs instead of three?

Then nobody knew the answer for over a century. A formula was conjectured by Frame and Stewart in 1941, and it was only proved to be optimal for four pegs by Thierry Bousch in 2014. For five pegs and more it is still open. The three-peg case being so clean is the exception, not the rule.

+ Is it free?

Yes, and there is no sign-up. Your best result for each disc count is kept in your own browser and nothing is sent anywhere.

More games like this

River Crossing — screenshot of the browser gameMath & Logic
Logic Puzzle1 Player

River Crossing

Four puzzles, three of them from the oldest surviving puzzle book. Getting everyone across is easy; doing it in the fewest crossings is the actual question.

  • All three of Alcuin's river problems from around 800, plus missionaries and cannibals
  • A light says whether you are still on a shortest route — computed, not guessed
  • The wolf and goat puzzle has exactly two solutions, and we show the whole state graph
Play River Crossing
Peg Solitaire — screenshot of the browser gameMath & Logic
Solitaire Puzzle1-2 Players

Peg Solitaire

Thirty-three holes, one rule and exactly thirty-one jumps — never more, never fewer. What varies is only whether you strand yourself before the end.

  • Undo as far back as you like — this is a puzzle, not a test
  • Always exactly 31 jumps, and we show why that is not a coincidence
  • A colouring rules out 28 of the 33 possible endings before you move
Play Peg Solitaire
MENACE — screenshot of the browser gameMath & Logic
Machine Learning1 Player

MENACE

A machine built from 304 matchboxes that learns noughts and crosses by throwing away the beads for moves that lost. It starts knowing nothing. Play it and watch it change.

  • The 1961 original, rule for rule — beads, matchboxes and all
  • Watch the box it opens and the beads it gains and loses
  • Train it against a human, a random player or perfect play
Play MENACE
Dots and Boxes — screenshot of the browser gameMath & Logic
Pencil Game2 Players

Dots and Boxes

Draw a line, close a box, go again. The rules take twenty seconds and the strategy takes years — because the winning move is usually the one that gives boxes away.

  • Three grid sizes and three computer levels
  • We solved the small boards exactly — the numbers are on this page
  • Two players on one device, no account needed
Play Dots and Boxes