Tower of Hanoi facts at a glance
The Tower of Hanoi on this page lets you play with 3 to 8 disks, and its solver can finish any position you reach, not only the starting stack. It is free, needs no account and runs in your browser.
| Disks | 3 to 8, starting on peg A; the goal is peg C |
|---|---|
| Fewest moves | 2n − 1 for n disks: 7 for 3, 15 for 4, 31 for 5, 63 for 6, 127 for 7, 255 for 8 |
| Rules | Move one disk at a time, only the top disk of a peg, never onto a smaller disk |
| Hint | The next move of the shortest way to finish from your current position |
| Solver | Plays the fewest remaining moves from any legal position, and lists them |
| 64 disks | 18,446,744,073,709,551,615 moves, about 585 billion years at one move per second |
| Price and setup | Free, no account, no download; runs in your browser |
How to play the Tower of Hanoi
Move the stack from peg A on the left to peg C on the right. Tap a peg to lift its top disk, then tap the peg where it should go.
- Pick the number of disks. Start with 3 disks (7 moves) and work up to 8 (255 moves).
- Lift a disk. Tap a peg; its top disk lifts up. Tap the same peg again to put it back.
- Drop it. Tap another peg. The disk lands if that peg is empty or its top disk is bigger.
- Ask for help when you need it. Hint marks the best next move from your position. Undo takes back your last move.
- Let the solver finish. Solve from here plays the fewest remaining moves and lists them, so you can follow along or copy them.
The Tower of Hanoi solution, step by step
With 3 disks the shortest solution has 7 moves: disk 1 from A to C, disk 2 from A to B, disk 1 from C to B, disk 3 from A to C, disk 1 from B to A, disk 2 from B to C, and disk 1 from A to C. Disk 1 is the smallest.
The same idea works for any number of disks. To move a stack of n disks, first move the top n − 1 disks out of the way to the spare peg, then move the biggest disk to the goal, then move the n − 1 disks on top of it. Each extra disk doubles the work and adds one, which is why n disks take 2n − 1 moves: 15 for 4, 31 for 5, and 255 for 8.
A trick you can do without thinking
Every other move is the smallest disk, and it always travels in the same direction round the three pegs. With an odd number of disks it goes A to C to B to A; with an even number, A to B to C to A. On the moves in between, there is only one legal move that does not touch the smallest disk, so make that one. Follow these two rules and you will finish in the fewest moves without planning ahead.
Fixing a game that went wrong
If you have made extra moves, you do not need to start over. The solver works from any legal position: it looks at the biggest disk that is not yet on peg C, clears every smaller disk onto the spare peg, moves the big disk across, and repeats. That is always the shortest way to finish from where you are, and it is what Hint and Solve from here use.
Where the puzzle comes from
The French mathematician Édouard Lucas published the Tower of Hanoi in 1883, with a legend about priests moving 64 golden disks: when they finish, the world ends. At one move per second without a mistake, 64 disks would take 18,446,744,073,709,551,615 moves, about 585 billion years.
Frequently asked questions
What is the fewest number of moves for the Tower of Hanoi?
2 to the power of the number of disks, minus 1. That is 7 moves for 3 disks, 15 for 4, 31 for 5, 63 for 6, 127 for 7 and 255 for 8.
What is the solution for 3 disks?
Disk 1 from A to C, disk 2 from A to B, disk 1 from C to B, disk 3 from A to C, disk 1 from B to A, disk 2 from B to C, disk 1 from A to C. Disk 1 is the smallest.
Can the solver finish a game I already started?
Yes. Hint and Solve from here work from any legal position, not only from the start, and always use the fewest moves left.
Does the stack have to end on the right peg?
Here the goal is peg C on the right. In the puzzle itself any peg other than the start works the same way and takes the same number of moves.
Is there an easy rule to remember?
Move the smallest disk every other turn, always in the same direction round the pegs: A to C to B for an odd number of disks, A to B to C for an even number. In between, make the only legal move that does not use the smallest disk.
Is the Tower of Hanoi game free?
Yes. It is free, with no account and no download, and it runs in your browser.