Tower of Hanoi

Move a stack of discs from the first peg to the last, one disc at a time, and never set a bigger disc on a smaller one. Each extra disc roughly doubles the number of moves it takes.

How to play

Everything turns on the spare peg in the middle: to shift the biggest disc you first have to park every smaller one out of the way.

  1. Tap a peg to pick up You always take the top disc of that peg.
  2. Tap another peg to drop it If it would land on something smaller, the move is refused and you are told why.
  3. Move the whole stack to the right That wins.
Full instructions
In depth

Solving the Tower of Hanoi in the fewest moves

How the puzzle works, why the minimum number of moves is a fixed formula, the recursive trick that cracks any size, and how to play it here with three to seven discs.

Screenshot: How to play Tower of Hanoi
How to play Tower of Hanoi as it appears when the page opens.

What you are looking at

Three pegs stand side by side. A stack of coloured discs sits on the left peg, each one smaller than the one below it. The goal is to move the entire stack to the right peg. You may only move one disc at a time, and a larger disc can never sit on top of a smaller one. That single rule is the whole puzzle.

You choose how many discs to work with, from three up to seven. Three is a warm-up that takes under a minute. Seven is a serious test of patience and memory.

Controls and how the board responds

Tap a peg to pick up its top disc. That peg is then marked as held. Tap a second peg to drop the disc there. If the target peg already has a smaller disc on top, the move is refused and you get a short message explaining why. Tap the same peg again to put the disc back and change your mind.

Three numbers sit above the board. Moves counts every disc you place. Fewest possible shows the theoretical minimum for the number of discs you chose. Moves left counts down from a limit that is a little above the minimum, so you have some room for mistakes but not unlimited room.

The disc selector below the board lets you switch between three and seven discs at any time. Changing it starts a fresh game. The New game button does the same thing without changing the count.

Why the minimum is always exact

The minimum number of moves for n discs is two to the power of n, minus one. Three discs need seven moves. Four need fifteen. Five need thirty-one. Seven need one hundred and twenty-seven. This is not a guess or an estimate. It is a mathematical proof, and no method will ever do it in fewer.

The proof is short. To move n discs, you must first move the top n minus one discs to the spare peg. That is the same puzzle with one fewer disc. Then you move the bottom disc across. Then you move the n minus one discs again, from the spare peg to the target. Each of those sub-problems costs the same, and the bottom disc adds one move. The total comes to twice the previous answer plus one, which gives the formula.

The recursive trick, step by step

Think about four discs on three pegs, named left, middle, and right. The job is to get all four from left to right.

First, move the top three discs from left to middle. Ignore the bottom disc while you do this. The bottom disc is too large to get in the way of anything, so it may as well not exist. Moving three discs is a puzzle you already know how to solve.

Second, move disc four from left to right. It is now alone, so this is one simple move.

Third, move the three discs from middle to right. Again, the same smaller puzzle.

That is the entire method. Each step is either one move or a smaller copy of the same puzzle. When you reach one disc, the answer is obvious. When you reach two, you move the small one aside, move the big one, then put the small one on top. Everything larger is just more nesting of the same idea.

The smallest disc rule

There is a shortcut that saves a lot of thinking. On every odd-numbered move, the smallest disc moves. On every even-numbered move, one of the other discs moves. And the smallest disc always travels in the same direction around the three pegs.

For an odd number of discs, the smallest disc cycles left, middle, right, left, middle, right. For an even number, it goes the other way: left, right, middle, left, right, middle. Once you know which direction applies, half the moves are decided for you. On the even-numbered moves, there is only one legal move that does not involve the smallest disc, so the entire puzzle becomes mechanical.

This pattern holds for any number of discs. It is why experienced players can solve the puzzle at speed without pausing to think.

Scoring and the move limit

The game tracks your move count against the mathematical minimum. If you finish inside the move limit, the board tells you whether you hit the perfect score or how many extra moves you used.

The limit is set at the minimum plus a small allowance. For three discs the minimum is seven moves, so you have a few more on top before the game calls time. For seven discs the minimum is one hundred and twenty-seven, and the budget is larger in proportion.

When you run out of moves, the game ends with a hint about the recursive method. Your best score for each disc count is saved in your browser, so you can try to match the perfect figure across separate sessions.

Mistakes that waste moves

  • Moving a disc to the spare peg and then immediately moving it back. Two moves spent for no progress. Pick up a disc only when you know where it belongs.
  • Forgetting which sub-problem you are in. With five or more discs, you will be three or four levels deep in the recursion. Lose track and you start shuffling discs at random, which eats through the budget fast.
  • Trying to solve it from the big disc up. The puzzle works top-down, not bottom-up. Clear the small discs first, then move the big one, then bring the small discs back.
  • Ignoring the smallest-disc pattern. If you do not follow it, you will make legal moves that are correct in isolation but lead nowhere. The pattern is a rail that keeps you on the shortest path.
  • Starting on seven discs before mastering three. Seven discs is the same logic, but you have to hold it in your head for one hundred and twenty-seven moves. Three discs teaches the pattern in seven moves, and four in fifteen. Build the habit before adding size.

Working through three discs

Label the pegs A, B, and C. Discs are numbered one (smallest) to three (biggest). All start on peg A.

Move one: disc one goes to C. Move two: disc two goes to B. Move three: disc one goes from C to B, on top of disc two. Move four: disc three goes to C. Move five: disc one goes from B to A. Move six: disc two goes from B to C. Move seven: disc one goes from A to C.

Every disc is now on peg C, in order. That took exactly seven moves, which is two to the power of three minus one. No shortcut exists. If you follow the smallest-disc rule, the moves fall out in exactly this order without any choices to make.

Choosing your difficulty

Three discs is a puzzle you can solve in your head before touching the board. Use it to learn the pattern. Four discs takes fifteen moves and still fits comfortably in short-term memory. Five discs, at thirty-one moves, is where most people start needing the smallest-disc rule to stay on track.

Six discs needs sixty-three moves. Seven needs one hundred and twenty-seven. At these sizes the puzzle is not harder to understand, but it is harder to execute. The recursion is the same; you simply have more layers of it to hold in your mind. If you can do seven in the minimum, you have genuinely internalised the algorithm.

Everything stays on your own machine

The puzzle runs entirely in your browser. No moves are sent anywhere. There is no server keeping track of your score, your time, or how many attempts you have made. Your best result for each disc count is saved in local storage on your device, and it stays there until you clear your browser data. You can prove it by loading the page, going offline, and playing a full game. Everything still works because there was never anything on the other end. Close the tab and nothing is left behind except that one saved score.

Help

How to play Tower of Hanoi

Everything turns on the spare peg in the middle: to shift the biggest disc you first have to park every smaller one out of the way. Discs move one at a time, and only onto a larger disc or an empty peg. Three discs need seven moves at best, four need fifteen, and it roughly doubles from there.

Tap a peg to pick up

You always take the top disc of that peg.

Tap another peg to drop it

If it would land on something smaller, the move is refused and you are told why.

Move the whole stack to the right

That wins.

Watch the move counter

Fewest possible tells you the perfect score. Moves left is your budget.

Try more discs

Each extra disc roughly doubles the work. Seven discs takes at least 127 moves.

Runs on your device

The whole game is played inside this page. Nothing is uploaded, there is no account, and your best score is stored in this browser only. Once the page has loaded it works offline.

Good to know

Solve the smaller stack first

To move four discs across, first move the top three onto the spare peg, then move the big one, then bring the three back on top. It is the same problem, one size smaller.

The smallest disc moves every other turn

And it always moves in the same direction around the pegs. If you notice which, half the puzzle solves itself.

The minimum is exact

Three discs need 7 moves, four need 15, seven need 127. That is not an estimate — nothing can do it in fewer.

Start with three

Three discs takes under a minute and teaches the pattern. The pattern is identical at seven, just longer.

Common questions

Is it free?
Yes — no account, no limit on how many games you play, and no ads inside the game area itself. Ads on the page around it pay for the site.
Does it work on a phone?
Yes. The board scales to your screen and the controls are made big enough to use with a thumb.
What is the fewest possible moves?
Two to the power of the number of discs, minus one. So 7 moves for 3 discs, 15 for 4, and 127 for 7.
What is the strategy?
Move the top discs to the spare peg, move the biggest disc across, then move the smaller stack on top of it. Each step is the same puzzle with one fewer disc.
Why was my move refused?
A bigger disc can never go on a smaller one. That is the only rule.
Where is my best score kept?
In your browser on this device. It is never sent anywhere and will not follow you to another computer.