- Moves
- 0
- Minimum
- …
- Fewest from here
- …
- Time
- 0:00
Tower of Hanoi with 4 Pegs
Add a fourth peg and the puzzle changes completely: ten disks take 49 moves instead of 1,023. Here is how, and a board to try it on.
What a fourth peg changes
The rules are the same: one disk at a time, only the top disk, never a larger disk on a smaller one. The tower still starts on the first peg and has to end on the last. The extra peg gives you somewhere to park disks, and that saves an enormous number of moves, because every disk you can park is a disk you do not have to carry in two halves.
The four-peg version is often called Reve’s puzzle, after a puzzle in Henry Dudeney’s Canterbury Puzzles.
How many moves
| Disks | 3 pegs | 4 pegs | Saved |
|---|---|---|---|
| 1 | 1 | 1 | 0 |
| 2 | 3 | 3 | 0 |
| 3 | 7 | 5 | 2 |
| 4 | 15 | 9 | 6 |
| 5 | 31 | 13 | 18 |
| 6 | 63 | 17 | 46 |
| 7 | 127 | 25 | 102 |
| 8 | 255 | 33 | 222 |
| 9 | 511 | 41 | 470 |
| 10 | 1,023 | 49 | 974 |
The four-peg numbers grow in a pattern: from one disk to the next they go up by 1, 2, 2, 4, 4, 4, 8, 8, 8, 8, … A power of two appears as many times as its exponent plus one: once 1, twice 2, three times 4, four times 8, and so on. The three-peg numbers, by comparison, double every time.
The Frame–Stewart method
To move n disks with four pegs, pick a number k and then:
- Move the top k disks to a spare peg, using all four pegs (the same problem, smaller).
- Move the other n − k disks to the target peg using the three pegs that are left, the ordinary way: 2n−k − 1 moves. The parked disks sit on the fourth peg, out of the way.
- Move the k parked disks onto the target peg, again using all four pegs.
The cost is 2 · T(k) + 2n−k − 1, where T(k) is the four-peg count for k disks. The method is to pick the k that makes this smallest. For five disks, k = 2 gives 2 · 3 + 7 = 13.
| Disks (n) | Best k | Park and return: 2·T(k) | Rest, 3 pegs | Total |
|---|---|---|---|---|
| 2 | 1 | 2 | 1 | 3 |
| 3 | 1 | 2 | 3 | 5 |
| 4 | 1 | 2 | 7 | 9 |
| 5 | 2 | 6 | 7 | 13 |
| 6 | 3 | 10 | 7 | 17 |
| 7 | 3 | 10 | 15 | 25 |
| 8 | 4 | 18 | 15 | 33 |
| 9 | 5 | 26 | 15 | 41 |
| 10 | 6 | 34 | 15 | 49 |
Where two values of k tie, the smaller is shown. We checked every number in these tables, from 1 to 10 disks, against a search of every position of the puzzle, and the board above uses that search: it is exact for these sizes. For bigger towers and more pegs the method is the best known; whether it is always optimal is a harder question.
A complete solution for 4 disks
Nine moves, where the three-peg puzzle needs fifteen. This is one of several shortest solutions; the board finds a different one if you press Solve from here. Pegs are numbered 1 to 4 from the left.
- Disk 1: peg 1 → 2
- Disk 2: peg 1 → 3
- Disk 1: peg 2 → 3
- Disk 3: peg 1 → 2
- Disk 4: peg 1 → 4
- Disk 3: peg 2 → 4
- Disk 1: peg 3 → 1
- Disk 2: peg 3 → 4
- Disk 1: peg 1 → 4
Try it
On the board above, 4 pegs is already selected. Play it by hand and watch Fewest from here to see how you are doing, press Shuffle for a random position, or let the solver finish it. The rules of the classic version, with a worked example, are in how to solve the Tower of Hanoi, and the three-peg formula is derived on the formula page.