عربي

Tower of Hanoi Solution: The Simple Rule and the Minimum Moves

Three glowing pegs with coloured disks and a small disk flying between them along a dashed arc

The Tower of Hanoi looks tricky the first time you see it, but it hides one of the most elegant patterns in mathematics. Once you know the rule, you can solve any tower perfectly. This guide explains the Tower of Hanoi solution step by step, the simple rule behind it and the formula for the minimum number of moves.

Follow along in our free Tower of Hanoi game, with 4, 6 or 8 disks.

The rules

  • There are three pegs. All the disks start stacked on the left peg, biggest at the bottom.
  • The goal is to move the whole tower to the right peg.
  • You may move only one disk at a time, and only the top disk of a peg.
  • A disk may never be placed on top of a smaller disk.

The 3-disk solution, step by step

Calling the pegs A (start), B (spare) and C (target), three disks can be moved in just seven moves:

Start1. disk 1: A → C2. disk 2: A → B3. disk 1: C → B4. disk 3: A → C5. disk 1: B → A6. disk 2: B → C7. disk 1: A → C
The perfect 3-disk solution in 7 moves, from peg A to peg C. The smallest disk moves on every odd-numbered move.

Notice two patterns. The smallest disk moves on moves 1, 3, 5 and 7, and it always travels in the same circular direction: A to C, C to B, B to A, then A to C again. On the even moves, there is only one legal move that does not touch the smallest disk.

The golden rule that solves any tower

Those patterns give a rule that works for any number of disks:

  1. Odd moves: move the smallest disk one step around the pegs, always in the same direction.
  2. Even moves: make the only legal move that does not involve the smallest disk.
  3. Choose the direction at the start: with an odd number of disks, the smallest disk goes first to the target peg; with an even number, it goes first to the spare peg.

Follow these three lines and you will always finish in the minimum number of moves.

Why it works: the recursive idea

The deeper reason is recursion. To move a tower of n disks, you only need to know how to move a tower of n − 1 disks:

Start:n disks on A1. Move n−1 disksto B2. Move the biggestdisk to C3. Move n−1 diskson top of it
The recursive idea: to move a whole tower, move the smaller tower out of the way, move the biggest disk, then move the smaller tower back on top.

The smaller tower is moved in exactly the same way, using an even smaller tower, and so on down to a single disk. That is why the Tower of Hanoi is a favourite example in computer science classes.

The minimum number of moves

Because each extra disk means moving the smaller tower twice plus one move for the biggest disk, the minimum number of moves is 2ⁿ − 1, where n is the number of disks.

11327315431563612772558Disks (below) and minimum moves (above). Pink bars are the game modes.
Every extra disk roughly doubles the number of moves: 2ⁿ − 1.
DisksMinimum movesAt one move per second
377 seconds
41515 seconds
663about 1 minute
8255about 4 minutes
201,048,575about 12 days
6418,446,744,073,709,551,615about 585 billion years

The legend of the 64 disks

When the French mathematician Édouard Lucas published the puzzle in 1883, he added a story about priests in a great temple moving 64 golden disks between three diamond needles, with the world ending when they finished. As the table shows, even at one move every second it would take around 585 billion years, far longer than the age of the universe.

Tips for the 6- and 8-disk towers

  • Use the keyboard: keys 1, 2 and 3 pick and drop on each peg, which is much faster than dragging.
  • Say the rhythm: "small disk, other move, small disk, other move" keeps you on track.
  • Check the direction: 6 and 8 are even, so the smallest disk starts by moving to the middle peg.
  • Don’t panic in the middle: half-way through, the biggest disk moves to the right peg. After that you simply rebuild the smaller tower on top of it.

Frequently asked questions

What is the minimum number of moves for the Tower of Hanoi?

For n disks the minimum is 2ⁿ − 1 moves: 7 for three disks, 15 for four, 31 for five, 63 for six, 127 for seven and 255 for eight.

What is the easiest way to solve the Tower of Hanoi?

Move the smallest disk on every other turn, always around the pegs in the same direction. On the turns in between, make the only legal move that does not use the smallest disk.

Where should the smallest disk go first?

If the number of disks is odd, move the smallest disk straight to the target peg. If it is even, move it to the spare peg first.

Who invented the Tower of Hanoi?

The French mathematician Édouard Lucas published the puzzle in 1883, together with a legend about priests moving 64 golden disks.

Why is the Tower of Hanoi used to teach recursion?

Because the solution for n disks is built from the solution for n − 1 disks, it is one of the clearest examples of a problem that solves itself by repeating a smaller version of the same steps.

Play Tower of Hanoi

Try the golden rule in our free Tower of Hanoi game and aim for 15 moves with four disks. Love logic puzzles? Try Slide Puzzle and Crate Pusher, read our Code Breaker strategy, or see more brain games for adults.

Games to try