Tower of Hanoi
Tap a peg to lift its top disc, then tap another peg to drop it. On a computer you can also drag, or press A, B and C.
What is Tower of Hanoi?
Tower of Hanoi is a free online puzzle game that teaches recursion, one of the big ideas in computer science. Lift glossy discs off wooden pegs and move the whole tower from peg A to peg C, one disc at a time, never putting a bigger disc on a smaller one. Start with 3 discs and unlock up to 8, aiming for the minimum number of moves: 2 to the power of the number of discs, minus 1. A live recipe card ticks off the three big steps as you play, and Watch mode lets the computer solve it while a call stack shows how the recursive function works.
How to play Tower of Hanoi
Controls: Tap a peg to lift its top disc, then tap another peg to drop it. On a computer you can also drag, or press A, B and C.
- 1Tap peg A to lift its top disc, then tap another peg to drop it there. Glowing rings show where it is allowed to go.
- 2Only one disc moves at a time, and a bigger disc can never sit on a smaller one.
- 3Move the whole tower onto peg C, the one with the gold star.
- 4Stuck? Press Hint for the best next move, or Undo to take a move back.
- 5Solve it to unlock a tower with one more disc, up to 8. Try to match the minimum number of moves for 3 stars.
- 6Open Watch the algorithm to see the computer solve it step by step, with the call stack beside it.
Tower of Hanoi tips and strategy
- With an odd number of discs, move the smallest disc straight to the goal peg first. With an even number, move it to the middle peg first.
- The smallest disc moves on every other turn, and always travels around the pegs in the same direction.
- In between, there is only ever one legal move that does not use the smallest disc, so make that one.
- Think in big steps: get the top discs onto the spare peg, move the biggest disc to the goal, then rebuild the rest on top of it.
Tower of Hanoi FAQ
What is recursion?
Recursion is when a solution uses a smaller copy of itself. To move 5 discs you move the top 4 out of the way, move the biggest disc, then move the 4 back on top. Moving 4 discs is the same puzzle, only smaller, so the same recipe is used again and again until only one disc is left.
How many moves does the Tower of Hanoi take?
The fewest possible moves is 2 to the power of the number of discs, minus 1: 7 moves for 3 discs, 15 for 4, 31 for 5, 63 for 6, 127 for 7 and 255 for 8. Every extra disc doubles the work and adds one.
Who invented the Tower of Hanoi?
The French mathematician Edouard Lucas published it in 1883, together with a legend about monks moving 64 golden discs. At one move per second, 64 discs would take about 585 billion years.
Is the Tower of Hanoi good for kids?
Yes. The 3-disc tower can be solved in about a minute, the Hint button always shows the best next move, and each extra disc adds a real challenge. It builds planning skills and introduces an idea programmers use every day.