The disk-stacking puzzle, in your browser
This Tower of Hanoi online works with a mouse, a finger or the keyboard. A stack of disks of different sizes sits on the left peg. Your job is to rebuild the same stack, in the same order, on the right peg. Pick anything from 3 to 10 disks and play with "3 pegs", the classic puzzle, or "4 pegs" for a shorter variant. "Moves" counts every disk you move, "Minimum" shows the target, and the clock starts with your first move.
Rules and controls
- Move one disk at a time, always the top disk of a peg.
- Never place a disk on a smaller one. An illegal move is refused and a message tells you why.
- Drag the top disk to another peg, or tap the peg you are moving from and then the peg you are moving to.
- On a keyboard, focus the board: keys 1 to 4 pick the starting peg and then the target peg. Arrow keys and Enter work too, and Esc cancels.
An unfinished game is still there when you come back. The "Best scores" table keeps, for each number of disks and pegs, the games won, the fewest moves and the best time.
How many moves does it take?
With 3 pegs, the minimum is 2 to the power of the number of disks, minus 1, written 2^n − 1. The reason is simple. To move the largest disk, every other disk must first be parked on the middle peg. Then the big disk goes to the right, and the smaller ones are stacked back on top. Each extra disk therefore doubles the work, plus one move.
| Disks | 3 pegs | 4 pegs |
|---|---|---|
| 3 | 7 | 5 |
| 4 | 15 | 9 |
| 6 | 63 | 17 |
| 10 | 1,023 | 49 |
At one move per second, a perfect 10-disk game takes 17 minutes and 3 seconds. In a 4-disk tower, the smallest disk moves 8 times, the next 4 times, then 2 times and the largest just once: 8 + 4 + 2 + 1 = 15. Those are powers of two, and binary is the neatest way to read the game. On move 12, which is 1100 in a binary converter, disk 3 moves, because the number ends in two zeros.
A strategy that always works
- The smallest disk moves every other turn, always around the pegs in the same direction. With an odd number of disks, its first move is to the right-hand peg; with an even number, to the middle peg.
- Between two moves of the smallest disk, only one other move is legal. Make it.
With 4 pegs, the usual method sets a few small disks aside on a free peg, moves the rest as if there were 3 pegs, then brings the first ones back. Nobody has proved that this is the best possible for every stack size, so the minimum shown is labelled "conjectured". If you like puzzles that reward planning ahead, try the 15 puzzle or peg solitaire.
The animated solution
"Show solution" plays the moves one by one at the pace set in "Solution speed". From the starting position it uses exactly the minimum number of moves. In the middle of a 3-peg game it continues from where you are, by the shortest route; with 4 pegs it restarts the game. "Pause" and "Next move" let you follow each step. If your device asks for reduced motion, nothing plays on its own: each move waits for a click on "Next move". A game where the solution was used does not count towards best scores.
A little history
The puzzle was sold in France in the late nineteenth century, in 1883 according to most sources, along with a story about monks in a temple moving 64 golden disks. That story seems to have been written to sell the toy, and nothing shows a real link with the city of Hanoi.
Frequently asked questions
How long would the 64-disk legend take?
It needs 18,446,744,073,709,551,615 moves. At one move per second, that is roughly 585 billion years.
Can I finish on the middle peg?
No, the tool expects the whole stack on the right-hand peg. By symmetry, the minimum would be exactly the same.
How many disks should a child start with?
Three disks and 7 moves are perfect for learning the rule. Move up to 4 or 5 once that feels easy.
Why is the timer not running?
It starts on your first move and stops when the tower is complete. After a page reload, it picks up again on your next move.