Tower of Hanoi is the classic mathematical puzzle invented in 1883. Move all the disks from the leftmost tower to the rightmost, one at a time, never placing a larger disk on a smaller one.
Tower of Hanoi
Click a peg to pick up its top disc. Click another peg to drop. RULE: larger disc cannot go on smaller. Goal: move the whole stack to the RIGHT peg.
About Tower of Hanoi
The Tower of Hanoi is one of the most famous puzzles in computer science and mathematics. Three towers (rods) start with N disks of different sizes stacked on the leftmost rod, in size order with the largest on the bottom. The goal is to move all disks to the rightmost rod, following two rules: only one disk can move at a time, and a larger disk can never be placed on a smaller one. The minimum number of moves to solve N disks is 2^N – 1: 7 moves for 3 disks, 15 for 4, 31 for 5, 63 for 6, and 127 for 7. The puzzle is the canonical example of recursive problem-solving and is taught in nearly every computer science curriculum.
How to Play Tower of Hanoi
- Click any tower to “pick up” its top disk. The selected tower is highlighted.
- Click another tower to drop the disk there. You can only drop a disk onto an empty tower or onto a larger disk.
- You cannot place a larger disk on a smaller one. Such moves are silently rejected.
- Continue until all disks are stacked on the rightmost tower in size order.
- Adjust the disk count (3-7) using the dropdown. Each disk count has a known optimal move count.
Tips and Strategy
- For N disks, the optimal solution is 2^N – 1 moves. 3 disks = 7 moves, 4 disks = 15, 5 disks = 31, 6 disks = 63, 7 disks = 127.
- Recursive strategy: To move N disks from A to C, first move N-1 disks from A to B, then move the largest disk from A to C, then move N-1 disks from B to C.
- For odd N: the smallest disk always alternates between rods A->C->B->A (the “even” cycle). For even N: A->B->C->A.
- Never move the same disk twice in a row. After moving the smallest disk, you must move a different one.
- On every other move, you must move the smallest disk. The other moves are forced — there is only one legal non-smallest move.
- Start with 3 disks to learn the pattern, then increase. Once you understand the recursion, any disk count is solvable.
- Time the moves: the smallest disk moves every other turn. Use this to verify your moves are on track.
History and Origin
The Tower of Hanoi was invented in 1883 by French mathematician Édouard Lucas, who packaged it as a wooden toy and sold it in Paris. He attached a fictional legend that monks at a Vietnamese temple were solving a 64-disk version, and that the world would end when they finished. With 64 disks, the optimal solution is 2^64 – 1 ≈ 1.8 × 10^19 moves — at one move per second, that is 585 billion years, far longer than the age of the universe. The Tower of Hanoi is now a foundational example in computer science, used to teach recursion, induction, and mathematical analysis.
Variations and Game Modes
Variants include the “Frame–Stewart” version with 4+ rods (move count is reduced), the cyclic Tower (only adjacent rod moves allowed), the magnetic Tower (some moves blocked by parity), and physical implementations with wooden disks of various sizes for tactile play.
Frequently Asked Questions
How do you solve the Tower of Hanoi?
Use recursion: to move N disks from rod A to rod C using rod B, (1) move N-1 disks from A to B, (2) move the largest disk from A to C, (3) move N-1 disks from B to C. Repeat recursively until N=1, which is just one direct move.
What is the minimum number of moves to solve Tower of Hanoi?
For N disks, exactly 2^N – 1 moves. So 3 disks = 7 moves, 4 disks = 15, 5 = 31, 6 = 63, 7 = 127, 10 = 1023, 64 = 18,446,744,073,709,551,615 moves.
How long would the legendary 64-disk version take?
At one move per second, 2^64 – 1 moves takes about 585 billion years — far longer than the current age of the universe (~14 billion years). This is the basis for the legend that the world ends when the monks finish.
Why is the Tower of Hanoi taught in computer science?
Because it is the canonical example of recursive problem-solving. The recursive solution is short, elegant, and demonstrates how a hard problem can be reduced to two smaller versions of itself. It also teaches inductive proof and complexity analysis.
Can I solve Tower of Hanoi without recursion?
Yes. There is a non-recursive iterative solution: alternate between (a) moving the smallest disk one step, and (b) making the only other legal move. This produces the optimal sequence without conscious recursion.
What is the rule about disk size?
A disk can never be placed on top of a smaller disk. So disks can only be stacked in strictly decreasing size order from bottom to top.
Does the Tower of Hanoi change with more disks?
The strategy is the same regardless of disk count. The total number of moves doubles (plus one) each time you add a disk: N+1 disks need 2^(N+1) – 1 = 2 × (2^N – 1) + 1 moves.
Can children play Tower of Hanoi?
Yes. With 3 disks (7 moves), Tower of Hanoi is suitable for ages 5+. With 4-5 disks it becomes a real challenge. The recursive insight typically clicks around age 12+.