The fastest solution to the Tower of Hanoi with n disks takes exactly 2ⁿ − 1 moves: 7 moves for 3 disks, 15 for 4, 31 for 5 and 255 for 8. You can find every one of those moves with a single rule: move the smallest disk one step around the pegs in the same direction every time, and in between make the only other legal move. Below is why that works, the full 3-disk solution, and tips for bigger towers.
The rules
The puzzle has three pegs and a stack of disks, biggest at the bottom. The goal is to move the whole stack from the left peg to the right peg.
- Move one disk at a time.
- Only the top disk of a peg can move.
- Never put a bigger disk on top of a smaller one.
In our Tower of Hanoi the disks are smooth river stones in a quiet stone garden, but the rules are exactly the same.
How many moves does it take?
| Disks | Fewest moves | At one move a second |
|---|---|---|
| 3 | 7 | 7 seconds |
| 4 | 15 | 15 seconds |
| 5 | 31 | 31 seconds |
| 6 | 63 | about 1 minute |
| 7 | 127 | about 2 minutes |
| 8 | 255 | about 4 minutes |
Each extra disk doubles the work, plus one. To move the biggest disk to the right peg, every smaller disk first has to be out of the way, stacked on the middle peg. So you solve the smaller tower once to clear the way, move the biggest disk once, then solve the smaller tower again on top of it. That gives moves(n) = 2 × moves(n − 1) + 1, which works out to 2ⁿ − 1. No shorter solution exists, because the biggest disk can't move until the smaller tower has been moved out of its way at least once, and moved back on top of it at least once.
The one rule that solves any tower
- Move the smallest disk one peg along, always in the same direction. With an odd number of disks it travels left, right, middle, left, and so on. With an even number it travels left, middle, right, left.
- Make the only legal move that doesn't touch the smallest disk. There is always exactly one.
- Repeat steps 1 and 2 until the tower is on the right peg.
The smallest disk moves on every other turn, so with 3 disks it moves 4 of the 7 times. In general the smallest disk moves 2ⁿ⁻¹ times, the next one half as often, and the biggest disk moves only once.
The 3-disk solution, move by move
- Smallest disk from the left peg to the right peg.
- Middle disk from the left peg to the middle peg.
- Smallest disk from the right peg to the middle peg.
- Biggest disk from the left peg to the right peg.
- Smallest disk from the middle peg to the left peg.
- Middle disk from the middle peg to the right peg.
- Smallest disk from the left peg to the right peg.
Notice the pattern: moves 1 to 3 move a 2-disk tower to the middle, move 4 moves the biggest disk, and moves 5 to 7 move the 2-disk tower back on top of it.
Tips for bigger towers
- Think about the biggest disk first. Before it can move, every other disk must be stacked on the one peg it isn't going to.
- Lost your place? You don't have to start over. From any legal position there is a shortest way to finish, and in our game the Hint button shows its next move.
- Count against par. Our game shows par (2ⁿ − 1) for every stack. Solve at par for three stars, and use Undo freely while you learn the pattern.
Where the puzzle comes from
The French mathematician Édouard Lucas published the puzzle in 1883. It came with a legend about a temple where priests move 64 golden disks, and the world ends when they finish. At one move a second, 64 disks take 2⁶⁴ − 1 moves, which is more than 580 billion years, so there's no need to hurry.
Our Tower of Hanoi starts at 3 stones and climbs to 8, with a Daily Stack that is the same for everyone each day and a relaxing Zen mode with no counters. It's free, there are no ads, and it starts the moment you tap.