How to Solve the Tower of Hanoi: A Step-by-Step Guide
Tower of Hanoi guide ยท 6 min read
The Tower of Hanoi looks intimidating with a tall stack of disks, but it is one of the most learnable puzzles ever invented. Once you see the single repeating pattern behind it, you can solve any size, from three disks to ten, without guessing. This step-by-step guide covers the rules, the move-by-move solution for three disks, and the recursive idea that scales up to every Tower of Hanoi puzzle. By the end you'll know exactly how to solve the Tower of Hanoi every time.
The rules in 30 seconds
The setup is three pegs and a stack of disks of different sizes, all on the left peg, largest at the bottom. Your goal is to move the whole stack to another peg. There are only three rules:
- Move one disk at a time.
- Only the top disk of a stack can move.
- Never place a larger disk on top of a smaller one.
That's the entire game. The official version is laid out on the Tower of Hanoi rules page, but those three rules are all you need to start.
The one idea that solves everything
Here is the secret. Don't think about moving the whole stack at once. Think about moving the biggest disk just once, from the start peg to the goal peg. For that to happen, every other disk has to be sitting on the spare peg, out of the way.
So solving the Tower of Hanoi is really three steps, no matter how many disks you have:
- Move the stack of all the smaller disks onto the spare peg.
- Move the single biggest disk to the goal peg.
- Move that stack of smaller disks from the spare peg onto the goal peg, on top of the biggest disk.
Steps 1 and 3 are just smaller Tower of Hanoi puzzles. That self-similarity is the whole trick, and it's why the puzzle is the classic example of recursion. We dig into the formal version in the Tower of Hanoi algorithm explained.
Step-by-step: solving 3 disks
Let's solve the easy 3-disk version move by move. Call the pegs A (start), B (spare), and C (goal). Number the disks 1 (smallest), 2, and 3 (largest).
- Move disk 1 from A to C.
- Move disk 2 from A to B.
- Move disk 1 from C to B (now disks 1 and 2 are stacked on B).
- Move disk 3 from A to C (the biggest disk reaches the goal).
- Move disk 1 from B to A.
- Move disk 2 from B to C.
- Move disk 1 from A to C.
Done. Seven moves, and the whole stack is on peg C. Notice how moves 1 to 3 cleared the small disks onto the spare peg, move 4 placed the giant, and moves 5 to 7 rebuilt the small stack on top. That is the three-step idea in action.
Why 3 disks always takes 7 moves
You may have noticed seven is not a random number. The minimum number of moves to solve the Tower of Hanoi is 2โฟ โ 1, where n is the number of disks. For three disks that's 2ยณ โ 1 = 7. For four disks it's 15, for five it's 31, and it keeps doubling. There's a full explanation in the Tower of Hanoi formula, but the takeaway is simple: if you solved three disks in seven moves, you solved it perfectly.
A shortcut you can do without thinking
If recursion feels abstract, there's an even simpler way to solve it by hand, often called the Tower of Hanoi trick. It comes down to alternating two kinds of moves:
- On every other move, move the smallest disk. Always slide it in the same circular direction. For an odd number of disks, move it start โ goal โ spare โ start and repeat. For an even number, move it start โ spare โ goal โ start.
- On the moves in between, make the only other legal move that doesn't involve the smallest disk. There is always exactly one.
Follow that rhythm and the puzzle solves itself, no planning required. You can see this pattern play out across the 3, 4 and 5 disk solutions.
Scaling up to more disks
The beautiful part is that nothing changes as the disks pile up. To solve 4 disks, you move the top 3 onto the spare peg (a 3-disk puzzle you already know), move disk 4 to the goal, then move those 3 back on top. Five disks wraps a 4-disk solution around the biggest disk, and so on. Each level is the same puzzle nested inside the next. We walk through the exact moves for 3, 4, and 5 disks in the disk-by-disk solutions guide.
This is also why the puzzle gets long but never genuinely harder. The 10-disk Einstein level needs 1,023 moves, but every one of them follows the same pattern as the 3-disk version. The challenge becomes focus and patience rather than cleverness.
Common mistakes to avoid
- Trying to move the biggest disk too early. It can only move when every smaller disk is parked on the spare peg. If the goal or spare peg has a small disk in the way, you've gotten ahead of yourself.
- Forgetting which peg is the spare. At each stage, the spare is whichever peg is neither your current source nor your current target. It changes as you recurse.
- Breaking the rhythm in the shortcut. If you use the alternating trick, never move the smallest disk twice in a row, and never reverse its direction.
Put it into practice
Reading the steps is one thing, feeling the pattern click is another. Open the 3-disk puzzle, solve it in seven moves, then jump to four and watch the same idea repeat. After a couple of rounds you won't need the steps at all, you'll just see the moves. Once you're comfortable, the algorithm guide shows how the same logic is written as code.
Frequently asked questions
What is the trick to solving the Tower of Hanoi?
The core trick is recursion: move every disk except the biggest onto the spare peg, move the biggest disk to the goal, then move the rest on top of it. For solving by hand, you can also alternate between moving the smallest disk (always in the same direction) and making the only other legal move.
How do you solve the Tower of Hanoi in 7 moves?
Seven moves is the perfect solution for three disks. Move the smallest disk to the goal, the middle disk to the spare, the smallest onto the middle, the biggest disk to the goal, the smallest back to the start, the middle onto the biggest, and finally the smallest onto the goal.
What is the minimum number of moves for the Tower of Hanoi?
The minimum is 2โฟ โ 1 moves, where n is the number of disks. So 3 disks take 7 moves, 4 disks take 15, 5 disks take 31, and 10 disks take 1,023. There is no way to solve it in fewer.
Can the Tower of Hanoi always be solved?
Yes. Every Tower of Hanoi puzzle has a guaranteed solution, and the same recursive pattern works for any number of disks. It can never become unsolvable as long as you follow the three rules.