๐Ÿ—ผ Tower of Hanoi

Click a peg to pick up, click another to place

๐Ÿ”ข Moves: 0 โญ Optimal: 7 โฑ 0:00 ๐Ÿ”ฅ 0

How to Play Tower of Hanoi

The Tower of Hanoi is a classic mathematical puzzle that has fascinated mathematicians, computer scientists, and puzzle lovers for over a century. The goal is deceptively simple: move a stack of discs from one peg to another, one disc at a time, without ever placing a larger disc on top of a smaller one. With just three discs it takes a minimum of 7 moves. With seven discs, you need at least 127 perfectly planned moves.

How to Play

Click on a peg to pick up its top disc (it will lift and highlight). Then click on another peg to place it there. You can only place a disc on an empty peg or on top of a larger disc. The puzzle is solved when all discs are moved from the leftmost peg to the rightmost peg. You can choose between 3 and 7 discs โ€” more discs means exponentially more moves required.

The optimal number of moves for n discs is 2โฟ - 1. The game displays this target so you can try to match it. Solving in exactly the optimal number of moves is the mark of a perfect solution.

History & Origin

The Tower of Hanoi was invented in 1883 by French mathematician ร‰douard Lucas. He sold it as a toy puzzle accompanied by a legend about monks in a temple moving 64 golden discs between three diamond pegs, following the same rules. According to the legend, when the monks complete the puzzle, the world will end โ€” which, at one move per second, would take over 584 billion years. The puzzle became a fundamental example in computer science for teaching recursive algorithms, as the optimal solution is naturally expressed as a recursive function.

Strategy & Tips

Frequently Asked Questions

How many moves does it take to solve Tower of Hanoi?

The minimum number of moves is 2โฟ - 1, where n is the number of discs. So 3 discs = 7 moves, 4 = 15, 5 = 31, 6 = 63, and 7 = 127 moves minimum.

Why is Tower of Hanoi important in computer science?

It is the textbook example of recursion โ€” a problem-solving technique where a function calls itself to break a problem into smaller sub-problems. Every computer science student learns the Tower of Hanoi as their introduction to recursive thinking.

Is there a non-recursive strategy?

Yes. An iterative approach exists: alternate between moving the smallest disc and making the only valid non-smallest-disc move. This yields the optimal solution without recursive thinking. For more puzzle strategies, see our best puzzle games roundup.

Play Tower of Hanoi free in your browser. Choose your disc count, plan your moves, and aim for the optimal solution.