Tap a peg to pick up its top disk, then tap the peg to move it to
What is the Tower of Hanoi?
There are three pegs and a stack of disks of different sizes. The goal is to move the whole tower to another peg while following the rules. The French mathematician Édouard Lucas introduced the puzzle in 1883, and it is still the first example many people meet when they learn recursion in programming.
How to play
Pick the number of disks (3 to 8). All disks start on the left peg.
Tap a peg to pick up its top disk (it is highlighted), then tap the peg you want to move it to. Tap the same peg again to cancel.
Get the whole stack onto the right-hand peg. Try to match the minimum number of moves shown above the board.
Stuck? Use Undo, or press Auto-solve to watch the computer do it step by step (games with help are not saved as a best record).
Only two rules
Move one disk at a time.
Never put a bigger disk on top of a smaller one.
Minimum number of moves
With n disks the minimum is 2ⁿ − 1 moves. Each extra disk doubles it (plus one).
Disks
Minimum moves
3
7
4
15
5
31
8
255
20
1,048,575
A legend tells of priests in a temple moving a tower of 64 golden disks, and when they finish, the world will end. Do the maths and that is about 18.4 quintillion moves: even at one move per second it would take more than 500 billion years, dozens of times the age of the universe. No need to worry.
How to solve it: think recursively
Moving n disks breaks down into three steps:
Move the top n−1 disks to the middle peg.
Move the biggest disk to the target peg.
Move the n−1 disks from the middle peg onto the target peg.
It is the same problem again, just one disk smaller, which is exactly what recursion means in programming. Press Auto-solve above to watch this happen.
A trick for solving by hand
Always move the smallest disk around the pegs in the same direction: to the right if the number of disks is even, to the left if it is odd. After each move of the smallest disk, there is only one other legal move, so make it. Alternate these two steps and you finish in the minimum number of moves.
FAQ
Is there a limit on undo?
No. But a game where you used undo or the auto-solve button is not saved as your best record.
What happens if I solve it in the minimum number of moves?
You get a "Perfect" message and the result is saved as your record.
How many disks can I play with?
You can choose from 3 to 8 disks. Eight disks need at least 255 moves.
What is the formula for the minimum number of moves?
With n disks the minimum is 2 to the power of n, minus 1. So 3 disks take 7 moves, 4 disks take 15 and 5 disks take 31. Every extra disk roughly doubles the work.