Play Tower of Hanoi online

Move the tower of disks to the right-hand peg, one disk at a time. 3 to 10 disks, a counter against the 2^n - 1 minimum, undo and an auto-solver.

How it works

  1. Pick the number of disks in the menu, from 3 to 10
  2. Tap a peg to lift its top disk, then tap the peg to drop it on; keys 1, 2 and 3 work too
  3. Move the whole tower to the right-hand peg, never putting a larger disk on a smaller one

The rules and the minimum number of moves

To solve the Tower of Hanoi, move the whole stack of disks from the left peg to the right peg, one disk at a time, never placing a larger disk on a smaller one. Only the top disk of a peg can be lifted, and the middle peg is yours to use as a parking place. The smallest number of moves for n disks is 2^n - 1: 7 moves for 3 disks, 15 for 4, 31 for 5, 63 for 6 and 1,023 for 10. The page shows your move count beside that number, so you can see how close you are. The puzzle was published by the French mathematician Edouard Lucas in 1883. The proof of the minimum is short: to move n disks you must first move the n - 1 disks above the largest one to the spare peg, then move the largest, then move the n - 1 disks back on top, which gives 2(2^(n-1) - 1) + 1 = 2^n - 1 moves. When you try to make an illegal move, the page tells you why instead of silently ignoring it.

The recursive solution

Solving the puzzle is a standard example of recursion, a problem solved by using a smaller copy of itself. To move k disks from peg A to peg C using peg B: move k - 1 disks from A to B using C, move the biggest disk from A to C, then move k - 1 disks from B to C using A. A tower of one disk is a single move, which is where the recursion stops. The Solve button follows that rule. Because it works from the position on the board, it can finish any legal position, not only the start, and it uses the fewest moves from there. You can watch it at Slow, Normal or Fast speed, set in the menu, and press Stop at any point to take over. A game finished with the solver is not counted towards your best. A quick pattern for playing by hand with an even number of disks: move the smallest disk to the middle peg first, and then alternate moving the smallest disk one peg onward in a fixed direction and making the only other legal move. With an odd number of disks, move it to the right-hand peg first.

Playing, undo and records

Tap a peg to lift its top disk and tap another peg to drop it. Tap the same peg again to put it down. With a keyboard, press 1, 2 or 3 to choose a peg, or Tab to a peg and press Enter. Each disk shows its number, so sizes can be told apart without colour. Undo takes back moves one at a time to the start, and Restart begins again with the same number of disks. The clock starts on your first move and pauses while the tab is hidden or the solver is running. Your fewest moves and best time are kept for each number of disks, in this browser. This page does not offer four pegs, a dragging mode or an online leaderboard.

Why it takes so long with many disks

The number of moves doubles with every disk added. 10 disks need 1,023 moves, 20 disks would need 1,048,575 and 64 disks, the number in the old legend of the temple at Benares, need 18,446,744,073,709,551,615 moves. At one move per second that is more than 500 billion years. For play, 3 to 5 disks suit a first try, 6 or 7 suit a patient solver, and 8 to 10 are a test of staying careful. Counting moves is the fastest way to notice that you have lost track: if you are at 40 moves on 5 disks, which need 31, you have wasted some.

Frequently asked questions

What is the minimum number of moves in Tower of Hanoi?

2^n - 1 for n disks: 7 for 3 disks, 15 for 4, 31 for 5, 63 for 6, 127 for 7 and 1,023 for 10 disks. The page shows this next to your own move count.

What are the rules?

Move one disk at a time, only the top disk of a peg, and never place a larger disk on a smaller one. The goal is the whole stack on the right-hand peg. If you try an illegal move, the page says why.

How does the Solve button work?

It applies the recursive rule from the position on the board: park the smaller disks on the spare peg, move the next disk, then bring the smaller disks back on top. It uses the fewest moves possible from there, for instance 15 from the start with 4 disks.

Can I play with a keyboard?

Yes. Press 1, 2 or 3 to lift the top disk of that peg and then 1, 2 or 3 again to drop it. You can also Tab to a peg and press Enter or Space.

Does Undo or Solve affect my best score?

Undo takes the move off your count, so a taken-back move costs nothing. A game finished with Solve, or with the solver's help part of the way, is not counted for your best.

How many disks should I start with?

3 disks, which take 7 moves, to learn the pattern, and 4 or 5 disks next. 5 disks need 31 moves and 7 disks need 127, so the longer towers are mostly about keeping count and not losing your place.