Tower of Hanoi

Move the whole tower from the left peg to the right peg, one disk at a time. A larger disk can never sit on top of a smaller one.

Choose your settings and press Start

Disks

Rules

Report a bug

Only the site operators can read your report. It will not appear in public comments.

Do not include your name, school, address, contact details, or links. The game version and browser type are attached automatically. Privacy

ver 1.0.0 · Updated

Release notes
  1. · ver 1.0.0Added the version label and release notes.
  2. Added the English version.
  3. First published.

How to play the Tower of Hanoi

The Tower of Hanoi is a classic one-player puzzle made of three pegs and a set of disks of different sizes. At the start, all the disks are stacked on the left peg, largest at the bottom and smallest at the top, like a cone. Your goal is to rebuild that tower on the right peg by moving one disk at a time, without ever placing a larger disk on a smaller one. There are only two rules, yet the number of moves grows so quickly as you add disks that the puzzle has fascinated players, teachers and computer scientists for well over a century.

This online version lets you play with 3 to 8 disks, counts your moves against the minimum, times your solve, and can show you a hint for the next move or play the whole solution for you.

The start and the goal

Start and goal side by side. At the start, four disks are stacked on the left peg from largest to smallest. At the goal, the same four disks sit on the right peg in the same order.
Move the tower from the left peg to the right peg, keeping the same order.

Every game begins with the complete tower on peg 1, the leftmost peg. You win when the whole tower stands on the rightmost peg, labeled Goal, in exactly the same order. The middle peg is a spare: you can use it as often as you like to park disks along the way.

Tower of Hanoi rules

Where a disk can go. On the left, the smallest disk is lifted and can go on the empty peg or on the larger disk, both marked with green checks. On the right, the second smallest disk is lifted: it can go on the empty peg, but not on the smallest disk, which is marked with a red cross.
A disk can go on an empty peg or on a larger disk.

The official rules fit in two lines:

If you try an illegal move in this game, the disk gives a little shake and a message explains why (for example, “A larger disk can’t go on top of a smaller one”). Illegal attempts don’t count as moves, so feel free to experiment.

How to move the disks on screen

The game screen. Tapping peg 1 lifts the red disk above the peg and outlines peg 1 in blue. Pegs 2 and 3, where the disk can go, get dashed outlines. Tapping peg 3 moves the disk there.
① Tap a peg to lift its top disk ② Tap the peg where you want it.

Click or tap a peg and its top disk lifts up. Then click or tap the peg where you want to put it, and the disk slides across and drops into place. Pegs where the lifted disk is allowed are outlined with a dashed frame. Changed your mind? Tap the same peg again to put the disk back. You can also grab a disk with your finger or mouse and drag it to another peg. On a keyboard, press 1, 2 and 3 to choose pegs (and 4 when you play with four pegs).

The bar above the board shows your move count, the minimum for your number of disks, and a timer. The timer starts with your first move, so you can think as long as you like before you begin. Undo takes back your last move whenever you need it.

Minimum number of moves: the 2n − 1 formula

You can choose from 3 to 8 disks. For every number of disks there is a minimum number of moves that can’t be beaten, and it roughly doubles each time you add a disk.

DisksMinimum moves (3 pegs)Good for
37Your first game
415Learning the idea
531Easy once you see the pattern
663Needs concentration
7127Find your rhythm
8255A real challenge

With n disks the minimum is 2n − 1 moves: multiply 2 by itself once for each disk, then subtract 1. Three disks take 2 × 2 × 2 − 1 = 7 moves and four disks take 2 × 2 × 2 × 2 − 1 = 15. If you finish in exactly the minimum, the game says “Solved in the minimum moves!” and records it.

Tower of Hanoi variations

Two variants. On the left, a board with four pegs and four disks on the leftmost peg. On the right, the adjacent-only rule: the lifted disk can move from the left peg to the middle peg (green check) but not straight to the right peg (red cross).
Choose Custom in the settings to try these variants.

Choose Custom in the settings to play two well-known variations of the puzzle.

RuleWhat changes4 disks
Standard (3 pegs)A disk may move to any peg15
4 pegsOne more spare peg; move the tower to the 4th peg9
Adjacent onlyA disk may only move to a neighboring peg, so it has to pass the middle peg80

With four pegs the extra parking space cuts the number of moves dramatically: 5 moves for 3 disks, 13 for 5 disks and only 33 for 8 disks. The adjacent-only rule does the opposite. Its minimum is 3n − 1, so even 3 disks take 26 moves and 6 disks take 728, which is why this variant stops at six disks.

Tower of Hanoi solution and strategy

1. Learn the 7-move solution for 3 disks

The 7-move solution for 3 disks: move 1 from peg 1 to 3, move 2 from 1 to 2, move 3 from 3 to 2, move 4 from 1 to 3, move 5 from 2 to 1, move 6 from 2 to 3, move 7 from 1 to 3.
The shortest solution for 3 disks. This pattern is the building block for everything else.

The shortest solution for three disks is 1→3, 1→2, 3→2, 1→3, 2→1, 2→3, 1→3, where the numbers are pegs. Solve it a few times until your hands know it, and four disks and more become much easier.

2. Think about the largest disk first

The idea behind the 4-disk solution. Step 1: move the top three disks to the middle peg (7 moves). Step 2: move the largest disk to the right peg (1 move). Step 3: move the three disks from the middle peg onto it (7 moves). 15 moves in total.
To move the largest disk, every other disk has to be out of the way.

The largest disk can only move to the goal peg when every other disk is stacked on the middle peg. So a 4-disk puzzle splits into three steps: move the top three disks to the middle, move the largest disk to the right, then move the three disks from the middle on top of it. Moving three disks takes 7 moves, so the total is 7 + 1 + 7 = 15. Five disks take 15 + 1 + 15 = 31. Breaking a big problem into a slightly smaller copy of the same problem is the key idea of the Tower of Hanoi.

3. Your first move depends on the number of disks

To solve in the minimum number of moves, pay attention to where the smallest disk goes first. With an odd number of disks (3, 5, 7), move it to the goal peg on the right. With an even number (4, 6, 8), move it to the middle peg. Start the other way and you will have to take a detour somewhere.

4. The smallest disk always travels in the same direction

How the smallest disk cycles. With an odd number of disks it goes from peg 1 to 3, 3 to 2, and 2 to 1. With an even number it goes 1 to 2, 2 to 3, and 3 to 1.
Once you know which way the smallest disk cycles, you never get lost.

In the shortest solution, the smallest disk moves on every other turn and always cycles around the pegs in the same direction: 1 → 3 → 2 → 1 … with an odd number of disks, and 1 → 2 → 3 → 1 … with an even number.

5. Every other move has only one option

After moving the smallest disk, the next move must use a different disk, and there is always exactly one legal choice: look at the two pegs without the smallest disk and move the smaller of their top disks onto the other peg. Alternate “smallest disk in its fixed direction” with “the only other legal move,” and you can solve any number of disks in the minimum number of moves without thinking ahead. This simple routine is the iterative Tower of Hanoi algorithm.

6. The recursive algorithm

If you are studying programming, the Tower of Hanoi is the classic example of recursion, and the idea from tip 2 translates directly into code. To move n disks from peg A to peg C using peg B as the spare:

move(n, A, C, B):          # n disks from A to C, spare B
  if n == 0: return
  move(n − 1, A, B, C)     # clear the way
  move disk n from A to C  # the largest disk
  move(n − 1, B, C, A)     # rebuild on top

If M(n) is the number of moves for n disks, the steps give M(n) = 2 × M(n − 1) + 1 with M(1) = 1, which works out to exactly 2n − 1. It is also a neat proof that you can’t do better: the largest disk has to move at least once, and before and after that move the other n − 1 disks must be moved as a complete tower.

7. Use the hint and the demo when you are stuck

Press Hint to see an arrow for the next move on the shortest path from where you are now. Even if you have wandered off the best route, the hint always counts from your current position, and pressing Hint a second time makes the move for you. Demo plays the rest of the solution automatically at one of three speeds, and “Stop and take over” hands control back at any time. Games where you used a hint, the demo or undo aren’t counted toward your personal best.

Tower of Hanoi FAQ

What is the minimum number of moves?

With three pegs and n disks, the minimum is 2n − 1 moves: 7 for 3 disks, 15 for 4, 31 for 5, 63 for 6, 127 for 7 and 255 for 8.

How long would 64 disks take?

264 − 1 is 18,446,744,073,709,551,615 moves. At one move per second without a break, that would take about 585 billion years, far longer than the universe has existed so far.

What is the minimum with four pegs?

9 moves for 4 disks, 13 for 5, 17 for 6, 25 for 7 and 33 for 8. These numbers come from the Frame–Stewart algorithm. Whether it is really optimal for four pegs was an open question for decades, and a proof was announced in 2014. For five or more pegs it is still unproven.

When does the timer start?

With your first move, and it stops when the puzzle is solved. Time spent planning before your first move doesn’t count. Personal bests are saved on this device for each number of disks and each rule.

Can I stop and continue later?

Yes. Open the page again in the same browser on the same device and press Continue to pick up with the same disks and move count. Sound is off at first; press “Sound: off” to hear a click when a disk lands and a chime when you finish.

The history of the Tower of Hanoi

The Tower of Hanoi was invented by the French mathematician Édouard Lucas and sold as a toy in 1883. He first published it under a made-up name, “N. Claus de Siam,” which is an anagram of “Lucas d’Amiens.” The puzzle came with a legend: in a temple in India, priests were moving 64 golden disks between three diamond needles according to these rules, and when they finished, the world would end. The story was Lucas’s own invention, but as the 64-disk calculation above shows, nobody needs to worry.

Today the puzzle is used in math classes to explore patterns and powers of two, in computer science courses to teach recursion, and in psychology as a test of planning. Most of all, it remains a satisfying puzzle to solve: start with three disks, work your way up to eight, and see if you can hit the minimum every time.