61Games

The Towers of Hanoi

Move a stack of disks from one peg to another without ever putting a bigger disk on a smaller one. Play it, watch the best solution, count the moves and see the binary pattern inside.

Temple

Sixty four golden disks

There are three pegs. On the first peg sits a stack of disks, the biggest at the bottom. Move the whole stack to the last peg. You may move one disk at a time, and you may never put a bigger disk on a smaller one. The puzzle was sold in 1883 under the name N. Claus de Siam, which is an anagram of Lucas d’Amiens. It was made by the French mathematician Édouard Lucas.

A story came with it. In a temple, priests move 64 golden disks, and when they finish, the world ends. Four altars to try. Play the puzzle yourself. Watch the best solution. Count the moves for any number of disks. And see which disk moves on each move. The numbers are worked out by the page. Without a script each altar shows one picture or table.

First altar

Play it

Choose the number of disks. Press a peg to pick up its top disk, then press the peg where it should go. The page counts your moves and tells you the fewest possible.

Start with three disks. Seven moves is the fewest, and most people find it in a few tries.

ABC

Three disks on peg A. Move them all to peg C. The fewest moves is 7.

Second altar

Watch the best way

There is a short rule that solves any size. To move a stack of n disks: first move the n minus 1 disks above the biggest out of the way, then move the biggest, then move the n minus 1 disks back on top of it. To move those, use the same rule again with one disk fewer. The rule calls itself, which is called recursion.

Step through the best solution. The biggest disk moves only once, in the middle.

ABC

The best solution for three disks has 7 moves.

  1. Peg A to peg C
  2. Peg A to peg B
  3. Peg C to peg B
  4. Peg A to peg C
  5. Peg B to peg A
  6. Peg B to peg C
  7. Peg A to peg C

Third altar

Count the moves

Each extra disk doubles the work. To move n disks you move n minus 1 disks twice and the biggest disk once, so the fewest moves is two times the fewest for one disk less, plus one. That makes 2 to the power n, minus 1. Three disks need 7 moves, ten need 1,023 and twenty need 1,048,575.

Slide the number of disks. The page shows how long it would take at one move a second. For 64 disks that is about 585 billion years, many times the age of the universe.

The fewest moves and the time at one move a second for some numbers of disks
DisksFewest movesAt one move a second
111 second
377 seconds
53131 seconds
101,02317 minutes
201,048,57512 days
301,073,741,82334 years
6418,446,744,073,709,551,615585 billion years

20 disks need at least 1,048,575 moves. At one move a second that takes 12 days.

Fourth altar

Which disk moves

The best solution has a pattern. Write the move number in binary. The disk that moves is given by the position of the lowest 1. The smallest disk moves on every other move, the next on every fourth move, the next on every eighth, and so on. Disk number k moves 2 to the power n minus k times. This is connected to the Gray code, a way of counting that changes one bit at a time, as one disk moves at a time.

Slide through the moves of a puzzle with four disks and see the pattern.

For four disks, the move number in binary and the disk that moves
MoveIn binaryDisk that moves
100011
200102
300111
401003
501011
601102
701111
810004
910011
1010102
1110111
1211003
1311011
1411102
1511111

Move 1 is 0001 in binary. The lowest 1 is in the first place, so disk 1, the smallest, moves.

Key takeaways

  1. The Towers of Hanoi was sold in 1883 under the name N. Claus de Siam, an anagram of Lucas d’Amiens, Édouard Lucas.
  2. To move n disks you need at least 2 to the n minus 1 moves. Each extra disk doubles the work.
  3. The best way is recursive: move the pile above the biggest disk away, move the biggest, move the pile back.
  4. 64 disks need 18,446,744,073,709,551,615 moves, about 585 billion years at one move a second.
  5. The disk that moves is given by the lowest 1 in the move number written in binary, which links the puzzle to counting and the Gray code.

Sources

Most of these sites could not be opened while this page was written, so the facts were checked through search results that quote them. Lucas presenting the puzzle in 1883 as N. Claus de Siam, the anagram of Lucas d’Amiens, the legend of the temple with 64 golden disks (it is not clear whether Lucas invented it or was inspired by it), the fewest moves of 2 to the n minus 1, the 18,446,744,073,709,551,615 moves for 64 disks and the figure of about 585 billion years at one move a second, the recurrence of twice the fewest for one disk less plus one, and the link with binary counting and the Gray code each appeared in at least two places. Sources differ on the details of the legend, and one gives a different figure for the years, which is wrong by the arithmetic. The page does not say where the temple is. The counts and the times are computed on the page and the best solution is checked move by move by the page’s own code.