Tower of Hanoi: Rules, Solution and the Minimum Moves Formula
The Tower of Hanoi is a classic puzzle invented by the French mathematician Édouard Lucas in 1883. It is a favourite in maths and computer science classes because its solution is short, elegant and teaches an important idea called recursion. In this guide you will learn the rules, the fastest way to solve it, and the formula for the minimum number of moves.
The rules
There are three pegs. At the start, all disks are stacked on the left peg in order of size, with the largest at the bottom. The goal is to move the whole stack to another peg. You must follow three rules:
- Move only one disk at a time.
- Only the top disk of a stack can be moved.
- A larger disk may never be placed on top of a smaller disk.
The minimum number of moves
For n disks, the smallest possible number of moves is 2n − 1.
| Disks | Minimum moves |
|---|---|
| 3 | 7 |
| 4 | 15 |
| 5 | 31 |
| 6 | 63 |
| 7 | 127 |
Why? To move the biggest disk, every smaller disk must first be moved out of the way onto the spare peg. Then the big disk moves once. Then all the smaller disks must be moved back on top of it. If moving n−1 disks takes M moves, moving n disks takes M + 1 + M moves. Starting from 1 move for a single disk, this doubling-plus-one pattern gives 1, 3, 7, 15, 31... which is exactly 2n − 1.
The easy step-by-step method
You do not need to think recursively to solve it. This simple pattern always works:
- Move the smallest disk one peg in a fixed direction. If you have an odd number of disks and want to finish on the right peg, move it to the right first (left → right → middle → left...). With an even number of disks, move it to the middle first (left → middle → right → left...).
- Make the only legal move that does not involve the smallest disk.
- Repeat steps 1 and 2 until the tower is complete.
Following this pattern solves the puzzle in the minimum number of moves every time.
The recursive solution
Programmers describe the solution like this: to move n disks from peg A to peg C, first move n−1 disks from A to B, then move the largest disk from A to C, then move the n−1 disks from B to C. Each smaller step uses the same idea, until only one disk is left. This is why the puzzle is a common first example when students learn recursion.
The legend
Lucas sold the puzzle with a story about a temple where priests move 64 golden disks, and the world ends when they finish. Even at one move per second, 264 − 1 moves would take roughly 585 billion years - far longer than the age of the universe - so there is no need to worry.
Try it
Start with 3 disks in our free Tower of Hanoi game and aim for 7 moves, then work your way up to 7 disks. If you like this kind of puzzle, also try the 8-puzzle and Lights Out.


