Move the whole tower from peg A to peg C, one disk at a time, never putting a bigger disk on a smaller one. Drag a disk or click two pegs, then see how close you got to the minimum number of moves.
Moves0
Minimum15
Time0:00
A 1
B 2
C 3 target
Drag a disk, or click a peg and then another peg.
🏆
Solved!
Advertisement
Advertisement
How to use the Tower of Hanoi
Pick the number of disks (3 is easy, 8 needs 255 moves) and press New game.
Move a disk by dragging it to another peg, or click the peg you want to take from and then the peg you want to put it on. Keys 1, 2 and 3 work too.
Follow the one rule: a disk can only go on an empty peg or on a larger disk.
Rebuild the tower on peg C. Your move count is compared with the minimum possible, 2ⁿ − 1.
Stuck? Press Hint for the best next move, Undo to take a move back, or Solve to watch the optimal solution from where you are.
Minimum moves for each number of disks
The fewest moves needed to move a tower of n disks is 2n − 1. It comes from the recurrence T(n) = 2·T(n − 1) + 1 with T(1) = 1: you move the smaller tower out of the way, move the biggest disk once, and move the smaller tower back on top.
Disks
Minimum moves (2n − 1)
Smallest disk moves
Largest disk moves
3
7
4
1
4
15
8
1
5
31
16
1
6
63
32
1
7
127
64
1
8
255
128
1
10
1,023
512
1
64
18,446,744,073,709,551,615
263
1
In an optimal solution disk k (counting from the smallest, k = 1) moves exactly 2n − k times, which is why the smallest disk moves on every second turn.
How to solve the Tower of Hanoi
The recursive method
Move the top n − 1 disks from the start peg to the spare peg (using the target peg as the helper).
Move the largest disk from the start peg to the target peg.
Move the n − 1 disks from the spare peg onto the target peg (using the start peg as the helper).
Each of steps 1 and 3 is the same puzzle with one disk fewer, so you repeat the idea until only one disk is left. This is a classic first example of recursion in computer science.
The easy pattern to remember
Alternate moves. On odd-numbered moves (1st, 3rd, 5th…) move the smallest disk. On even-numbered moves make the only legal move that doesn't involve the smallest disk.
Keep the smallest disk going the same way. With an odd number of disks it cycles A → C → B → A…; with an even number it cycles A → B → C → A….
Disks on the same peg alternate parity. In an optimal solution an odd-numbered disk never lands directly on another odd-numbered disk, and even never on even.
Keyboard controls
Key
Action
1, 2, 3 (or A, B, C)
Pick up from / drop onto that peg
Esc
Put the lifted disk back
Ctrl + Z
Undo the last move
Tab, then Enter
Focus a peg and select it
Frequently asked questions
What are the rules of the Tower of Hanoi?
All disks start stacked on one peg, largest at the bottom. You must move the whole stack to another peg, moving only one disk at a time (the top disk of a peg) and never placing a larger disk on top of a smaller one.
What is the minimum number of moves?
The minimum for n disks is 2^n − 1. That is 7 moves for 3 disks, 15 for 4, 31 for 5, 63 for 6, 127 for 7 and 255 for 8. Every extra disk doubles the moves and adds one.
What is the trick to solving it?
Think recursively: to move n disks to a target peg, first move the top n − 1 disks to the spare peg, move the largest disk to the target, then move the n − 1 disks on top of it. A handy pattern is that the smallest disk moves on every other turn, always in the same direction around the pegs.
Which way should the smallest disk move first?
With an odd number of disks, move the smallest disk straight to the target peg first (A to C). With an even number, move it to the middle peg first (A to B). Following this makes the optimal solution easy to find.
Can the solver finish a game I already started?
Yes. Press Solve at any time and it computes the shortest sequence of moves from your current position to a complete tower on peg C, then plays it with animation. You can stop it whenever you like.
How long would 64 disks take?
In the legend of the temple puzzle, monks move 64 golden disks. That needs 2^64 − 1, or about 18.4 quintillion, moves. At one move per second it would take roughly 585 billion years.