How to Solve the Tower of Hanoi (Fewest Moves)
Move the smallest disk every other move, always in the same direction, to finish in 2n − 1 moves.
Play the Tower of Hanoi (free, 3–10 disks)Move the smallest disk every other move, always in the same direction
1234
Start: move all 4 disks to the right peg
4 disks: fewest 15 moves (24 − 1) · Play 4 disks
- Odd number of disks: the smallest disk goes left → right → middle
- Even number of disks: it goes left → middle → right
- On moves 2, 4, 6… make the only move that doesn’t use the smallest disk
3 disks take 7 moves, 4 take 15, 5 take 31
Disks are numbered from the smallest. L, M and R are the left, middle and right pegs.
3 disks (7 moves)
- 1L → R
- 2L → M
- 1R → M
- 3L → R
- 1M → L
- 2M → R
- 1L → R
4 disks (15 moves)
- 1L → M
- 2L → R
- 1M → R
- 3L → M
- 1R → L
- 2R → M
- 1L → M
- 4L → R
- 1M → R
- 2M → L
- 1R → L
- 3M → R
- 1L → M
- 2L → R
- 1M → R
5 disks (31 moves)
- 1L → R
- 2L → M
- 1R → M
- 3L → R
- 1M → L
- 2M → R
- 1L → R
- 4L → M
- 1R → M
- 2R → L
- 1M → L
- 3R → M
- 1L → R
- 2L → M
- 1R → M
- 5L → R
- 1M → L
- 2M → R
- 1L → R
- 3M → L
- 1R → M
- 2R → L
- 1M → L
- 4M → R
- 1L → R
- 2L → M
- 1R → M
- 3L → R
- 1M → L
- 2M → R
- 1L → R
Formula: n disks take 2n − 1 moves (15 for 4 disks)
With n disks, multiply 2 by itself n times and subtract 1.
| Disks | Fewest moves | At 1 move a second |
|---|---|---|
| 3 | 7 | 7 sec |
| 4 | 15 | 15 sec |
| 5 | 31 | 31 sec |
| 6 | 63 | 1 min 3 sec |
| 7 | 127 | 2 min 7 sec |
| 8 | 255 | 4 min 15 sec |
| 9 | 511 | 8 min 31 sec |
| 10 | 1,023 | 17 min 3 sec |
n disks take twice the moves for n − 1, plus 1
Before the largest disk can move, every disk above it has to be out of the way.
the middle: 7
the right: 1
7 more
= 7 × 2 + 1
Add 1 to each count and it doubles every time, so n disks take 2n − 1.
| Disks | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Fewest moves | 1 | 3 | 7 | 15 | 31 |
| Plus 1 | 2 | 4 | 8 | 16 | 32 |
As a recurrence, an = 2an−1 + 1 becomes an + 1 = 2(an−1 + 1), so an = 2n − 1.
Recursive algorithm: n − 1 disks, disk n, n − 1 disks
Move the top n − 1 to the spare peg, move disk n, then stack the n − 1 on top of it. In Python:
def hanoi(n, src, dst, via):
if n == 0:
return
hanoi(n - 1, src, via, dst)
print(n, src, "->", dst)
hanoi(n - 1, via, dst, src)
hanoi(3, "L", "R", "M")
It prints the 7 moves of the 3-disk answer. The rule at the top is the same, without recursion.
FAQ
- Where does the first move go?
- With an odd number of disks, the smallest goes to the right peg. With an even number, to the middle.
- Can it be done in fewer moves?
- No. Before and after the largest disk moves, all the other disks must be stacked on one peg.
- Which disk moves on move k?
- Write k in binary. The disk is 1 plus the number of zeros at the end: move 4 (100) moves disk 3.
- How many times does each disk move?
- The smallest makes about half of all moves (2n−1). Each larger disk moves half as often.
- Is there an easy way to remember it?
- In the shortest solution, odd-numbered disks never go on odd ones, and even never on even.
- How do I solve it from a mixed-up position?
- Largest disk not on the right peg: stack the smaller ones on the third peg, move it, repeat.
- What if the goal is the middle peg?
- The smallest disk goes the opposite way: with an odd number of disks, left → middle → right.
- What about four pegs?
- At least 1, 3, 5, 9, 13, 17, 25 and 33 moves for 1 to 8 disks (the jumps go 2, 2, 4, 4, 4, 8…).
- Can I practice here?
- Yes: 3 to 10 disks, plus a daily puzzle of five scattered disks. “Hint” shows the next best move.