Tower of Hanoi lesson: predict, play, explain
A free lesson for maths or computing. Learners predict how many moves the Tower of Hanoi needs, test their predictions in the game, then explain the pattern. It ends with recursion: a big problem solved by solving a smaller copy of itself.
- Time
- About 30 to 40 minutes
- Groups
- Pairs, or the whole class with one screen
- The game
- Tower of Hanoi on Puzzle Desk, in the browser
- Cost
- Free: no sign-in, no ads
Learner sheet
Two pages: the prediction table, a QR code to the game and the questions.
Teacher sheet
Two pages: the steps, a teaching note and the worked answers.
What learners do
- Spot a pattern in a sequence of numbers and describe it in words
- Explain why the pattern holds, from the way the puzzle works
- Meet recursion: solving a problem by solving a smaller copy of it
You need
- A phone, tablet, Chromebook or computer with a web browser, one per pair (or one screen at the front)
- The learner sheet, printed or on screen
- Pencils. Coins of three sizes help for Step 1
The puzzle
Three pegs and a tower of discs on the left peg. Move the whole tower to the goal peg on the right, one disc at a time. Only the top disc of a peg can move, and a disc may never sit on a smaller disc.
Step 1: Predict
About 10 minutes
Before anyone opens the game. With one disc it takes one move. Let learners work out two discs on paper or with three coins of different sizes, then predict the fewest moves for 3, 4 and 5 discs and write their guesses in the table.
- With 1 disc it takes 1 move. How many moves do 2 discs need at the fewest? Draw or act out each move.
| Discs | My prediction | Moves I took | Fewest possible |
|---|---|---|---|
| 1 | 1 | 1 | 1 |
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
Step 2: Play
About 15 minutes
Open the game with 3 discs and solve it, counting the moves. Then 4 and 5 discs. The game shows the fewest moves possible above the board and says “the fewest possible” when a solve matches it, so learners can record both numbers and try again to match it.
Each button opens the game with that many discs. For Q5 there is also 7 discs. The game’s own picker has a six-disc “Mixed tower” too, which starts from a scrambled position.
Step 3: Explain
About 10 minutes
Learners look for the pattern in the fewest moves, then explain it with the largest disc: the only way to move it is to clear every smaller disc onto the spare peg first.
- Look down the “fewest possible” column: 1, 3, 7, … How do you get from one number to the next?
- To move the largest disc of a 4-disc tower, where must the other 3 discs be? How many moves does it take to get them there?
- Use your answer to explain why 4 discs need 7 + 1 + 7 = 15 moves.
- Predict the fewest moves for 6 and 7 discs without playing. Then check 7 discs in the game.
- Write a rule for n discs. Can you write one that does not need the answer for n − 1?
Worked answers
For the teacher. The fewest moves for each tower:
| Discs | Fewest moves |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 7 |
| 4 | 15 |
| 5 | 31 |
| 6 | 63 |
| 7 | 127 |
- 3 moves: the small disc to the spare peg, the large disc to the goal, the small disc onto the large one.
- Double it and add one: 1 × 2 + 1 = 3, 3 × 2 + 1 = 7, 7 × 2 + 1 = 15, then 31. Each number is also one less than a power of two: 2 − 1, 4 − 1, 8 − 1, 16 − 1.
- All three must be on the spare peg, in order: the largest disc can only move to an empty peg, and nothing may be on top of it. Getting them there is the 3-disc puzzle, so it takes 7 moves.
- 7 moves to put the top three discs on the spare peg, 1 move for the largest disc, then 7 moves to bring the three back on top of it: 7 + 1 + 7 = 15.
- 63 for 6 discs (31 × 2 + 1) and 127 for 7 discs (63 × 2 + 1).
- fewest(n) = 2 × fewest(n − 1) + 1, starting from fewest(1) = 1. Without the previous answer: fewest(n) = 2n − 1.
Why 2n − 1
To move a tower of n discs: move the top n − 1 discs to the spare peg, move the largest disc to the goal, then move the n − 1 discs back on top of it. That is fewest(n − 1) + 1 + fewest(n − 1) moves.
Why can’t there be a shorter way? The largest disc has to move at least once. Just before its first move, every smaller disc must already sit on the third peg, which takes at least fewest(n − 1) moves. After its last move onto the goal, all those discs must be moved back on top of it: at least fewest(n − 1) moves again. So no solution is shorter than 2 × fewest(n − 1) + 1, and the method above takes exactly that many.
A pattern to spot while playing: the smallest disc moves on every second move, always round the pegs in the same direction. With an odd number of discs its first move is to the goal peg, with an even number to the spare peg. In between, there is only ever one legal move that doesn’t touch it.
Extension
The legend of 64 discs
Édouard Lucas, who published the puzzle in 1883, told of priests moving a tower of 64 golden discs. At one move a second, with no mistakes, how long would 64 discs take? (Hint: 210 is about 1,000.)
Answer: 264 − 1 = 18,446,744,073,709,551,615 moves. At one move a second that is about 585 billion years, roughly 42 times the age of the universe so far (about 13.8 billion years). With the hint: 264 = 24 × (210)6, which is about 16 × 1018 seconds, and a year is about 3 × 107 seconds.
Recursion in code
This procedure solves the whole tower by calling itself on a smaller tower. Trace move_tower(3, left, right, middle): write down every “move disc” line in order. How many are there?
move_tower(n, from, to, spare):
if n = 0: stop
move_tower(n − 1, from, spare, to)
move disc n from "from" to "to"
move_tower(n − 1, spare, to, from)Answer: 7 moves, the fewest possible: disc 1 left to right, disc 2 left to middle, disc 1 right to middle, disc 3 left to right, disc 1 middle to left, disc 2 middle to right, disc 1 left to right. Each call on n discs makes two calls on n − 1 discs, which is where 2 × fewest(n − 1) + 1 comes from.
Teaching note
- No age or level is set: adapt it to your class. A first lesson can stop at the table and the rule “double it and add one”; learners who are ready can explain why no shorter way exists and trace the recursive procedure.
- The game shows the fewest moves possible above the board, so collect predictions before the devices come out.
- In pairs, one learner moves and the other counts and checks the rule; swap for each tower. Learners can play by touch, with a mouse, or with the keyboard (arrow keys to choose a peg, Enter to lift and drop).
- Hint shows the next move of the shortest way from where the learner is, even after mistakes. Encourage a full attempt first. The game also shows a clock; speed doesn’t matter here.
- There is nothing to install and no sign-in, and there are no ads. Progress is saved only in the browser on that device.
Using and sharing this lesson
Free to print, copy and adapt for teaching, in class or at home, including in your own worksheets and slides. Please keep a link to nofussgames.com/learn/tower-of-hanoi-recursion, and please don’t sell it.
More puzzles to reason about: logic puzzles on No Fuss Games.