Tower of Hanoi
how it works
The idea with 3 discs, as the watch page runs it.
Three pegs stand in a row, A, B and C. A tower of discs sits on A, each disc smaller than the one under it. Move the whole tower to C. You move one disc at a time, always a top disc, and never put a disc on a smaller one. The method thinks about the biggest disc. To move it, every disc above it must first go to the spare peg, B. Then the biggest moves to C, and the rest move back on top of it. Each smaller tower moves the same way, with the pegs' jobs swapped: to take 2 discs to B, C is the spare. A single disc just moves. This page counts moves, one for each disc moved.
| move | disc | from | to | the pegs after |
|---|---|---|---|---|
| 1 | 1 | A | C | A: 3, 2; C: 1 |
| 2 | 2 | A | B | A: 3; B: 2; C: 1 |
| 3 | 1 | C | B | A: 3; B: 2, 1 |
| 4 | 3 | A | C | B: 2, 1; C: 3 |
| 5 | 1 | B | A | A: 1; B: 2; C: 3 |
| 6 | 2 | B | C | A: 1; C: 3, 2 |
| 7 | 1 | A | C | C: 3, 2, 1 |
step 1 of 11
Move all 3 discs from peg A to peg C, one disc at a time, never putting a disc on a smaller one. Peg B is there to help.
what it remembers to know where it is
- discs in this part
- 3
- from peg
- A
- to peg
- C
step 2 of 11
To move 3 discs from A to C, first move the 2 above the biggest out of the way, onto B. That is a smaller tower, moved the same way.
what it remembers to know where it is
- discs in this part
- 3
- from peg
- A
- to peg
- C
step 3 of 11
Move disc 1 from A to C.
what it remembers to know where it is
- discs in this part
- 1
- from peg
- A
- to peg
- C
step 4 of 11
Move disc 2 from A to B.
what it remembers to know where it is
- discs in this part
- 2
- from peg
- A
- to peg
- B
step 6 of 11
Move disc 3 from A to C: it is where it belongs, and it will not move again.
what it remembers to know where it is
- discs in this part
- 3
- from peg
- A
- to peg
- C
step 7 of 11
The biggest of those 3 is on C. Now move the 2 on B onto it, using A to help, the same way again.
what it remembers to know where it is
- discs in this part
- 3
- from peg
- A
- to peg
- C
step 10 of 11
Move disc 1 from A to C: it is where it belongs, and it will not move again.
what it remembers to know where it is
- discs in this part
- 1
- from peg
- A
- to peg
- C
step 11 of 11
All 3 discs are on C after 7 moves. That is twice the moves for one disc fewer, and one more. No way takes fewer.
what it remembers to know where it is
- discs in this part
- not yet
- from peg
- not yet
- to peg
- not yet
Where it says not yet, nothing is held there at that moment.
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- to move n discs from one peg to another, with the third peg to help:Python lines 9 and 17
- if n is 1, move that one disc, and stopPython lines 10, 11 and 12
- first move the n - 1 discs above the biggest onto the third pegPython line 13
- move the biggest disc to where it is goingPython line 14
- then move the n - 1 discs from the third peg onto itPython line 15
the same in Python
The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.
def tower_of_hanoi(n):
"""Move n discs from peg A to peg C, one at a time, never a disc on a smaller one.
n is a whole number from 1. Returns every move in order, each [disc, from, to],
with disc 1 the smallest.
"""
moves = []
def move(k, source, target, spare): # move discs 1 to k from source to target
if k == 1: # one disc simply moves
moves.append([1, source, target])
return
move(k - 1, source, spare, target) # the k - 1 above the biggest, onto spare
moves.append([k, source, target]) # the biggest, disc k, to where it is going
move(k - 1, spare, target, source) # the k - 1 from spare back onto it
move(n, "A", "C", "B") # all n discs from A to C, with B to help
return moves
how it scales
The plot counts the moves the method makes for discs, measured from 1 to 13.
Each mark up is ten times the one before, and each mark across adds the same number of discs. Each disc about doubles the moves, so each step across lifts the point by about the same height. From 3 discs to 13, the moves go from 7 to 8,191, about a thousand times: three marks up. So the points climb in a nearly straight line. Question 1 in section 4 works out why.
discs moved, ten times more at each mark up
, how many discs there are, the same step more at each mark to the right
- moving discs, the one run there is for that solid where measured by running it; dashed where its formula carries it on to 40
| discs moved | time | |
|---|---|---|
| 13 | 8,191 | about 8.2 microseconds |
| 27 | 134,217,727 | about 134 milliseconds |
| 40 | 1,099,511,627,775 | about 18 minutes |
The discs moved are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.
make these counts yourself
Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.
"""Count Tower of Hanoi's moves yourself, as the plot on this page does.
Run it with Python 3.12 or later. For each number of discs from 1 to 13 it
prints the moves the method makes, and 2 to the power n, less 1, beside them.
The counts are the same on any computer; only the time differs.
"""
def tower_of_hanoi(n):
"""Move n discs from peg A to peg C, one at a time, never a disc on a smaller one.
n is a whole number from 1. Returns every move in order, each [disc, from, to],
with disc 1 the smallest.
"""
moves = []
def move(k, source, target, spare): # move discs 1 to k from source to target
if k == 1: # one disc simply moves
moves.append([1, source, target])
return
move(k - 1, source, spare, target) # the k - 1 above the biggest, onto spare
moves.append([k, source, target]) # the biggest, disc k, to where it is going
move(k - 1, spare, target, source) # the k - 1 from spare back onto it
move(n, "A", "C", "B") # all n discs from A to C, with B to help
return moves
def count(n):
"""The same method on n discs, counting its moves and keeping none."""
if n == 1:
return 1
return count(n - 1) + 1 + count(n - 1)
if __name__ == "__main__":
print(f"{'discs':>5} {'moves':>9} {'2^n - 1':>9}")
for n in range(1, 14):
print(f"{n:>5} {count(n):>9} {2 ** n - 1:>9}")
best, worst and average
Best, worst and average usually name different inputs of one size. Here the input is alone, so each is one input and one run, the same every time. So the table has one column, under the name worst, and no average is worked out.
A case is one kind of input. A bound says how fast its cost grows as grows.
The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.
| worstmoving discs, the one run there is for that | |
|---|---|
| discs moved | |
| how the discs moved grow in each case | |
| tight, | |
| ceiling, | |
| floor, | |
extra memory: at most calls at once, one for each tower size still being moved, and the list of moves the code hands back.
Extra memory is the room it needs on top of the input itself, while it runs.
| discs | moves | from the row above |
|---|---|---|
| 1 | 1 | one disc simply moves |
| 2 | 3 | 2 times 1, plus 1 |
| 3 | 7 | 2 times 3, plus 1 |
| 4 | 15 | 2 times 7, plus 1 |
| 5 | 31 | 2 times 15, plus 1 |
| 6 | 63 | 2 times 31, plus 1 |
where the formula comes from
Three questions, each worked line by line. Question 1 is the count the plot draws.
the move: the rule
the line:why it is allowed: To move discs, the method moves the above the biggest out of the way, moves the biggest once, and moves the back on top. For 3 discs, 3 moves, then 1, then 3, which is 7.
the move: each disc in turn
the line:3 discs first half middle second half moves 1 to 3 4 5 to 7 which disc 1, 2, 1 3 1, 2, 1 From the run. Disc 3 moves once, in the middle. Each half moves discs 1 and 2 as a tower, so disc 2 moves twice and disc 1 four times. why it is allowed: By line 1, disc moves once, in the middle, and the tower above it moves twice. In each of those, disc is the biggest and moves once, so twice in all. Each splits the same way, so the third biggest disc moves 4 times, and on.
guess it, thenthe move: add them up
the line:why it is allowed: By line 2, disc moves once, disc twice, and on, doubling each time, down to disc 1. Disc 1 is doublings from disc , so it moves times. For 3, 1 + 2 + 4.
guess it, thenthe move: add the doublings
the line:where does this come from?
question 1 of the page doubling and halving sumsAdd . What is the total? the move: name the sum
the line:why it is allowed: Let be the doubling sum up to . For as 3, it is 1 + 2 + 4 + 8.
the move: double both sides
the line:why it is allowed: Each term doubled is the next one along, and becomes . Doubling both sides keeps them equal. The plan: the two sums share all but their ends.
the move: take line 1 away
the line:why it is allowed: Take from both sides of line 2. On the right, write out as line 1 does.
the move: tidy the left side
the line:why it is allowed: Two lots of less one lot is one lot.
the move: cancel what both brackets share
the line:when n is 3 1 2 4 8 16 first bracket 2 4 8 16 second bracket 1 2 4 8 left over take away 1 16 Line 4 when is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15. why it is allowed: Every term from 2 to is in both brackets and cancels, leaving less 1.
the move: the terms before the last
the line:why it is allowed: The sum of line 5 stopping one term sooner: line 5 with for . For 3, it is 1 + 2 + 4. At as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.
the move: tidy the power
the line:why it is allowed: One more than is . For 3, 1 + 2 + 4 is 7, one less than 8.
the move: read it
the line:why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under , twice the last.
why it is allowed: The box under this line shows a doubling sum from 1 is one less than its next term. Its lines 6 and 7 take this sum, ending at , to . For 3, 1 + 2 + 4 is 7, one less than 8.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 4, is , 1 less than . For 3, 7 is at most 8.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 5, with the fixed multiple 1. means at most a fixed multiple of , from some on.
guess it, thenthe move: split the power
the line:why it is allowed: By line 4, is , and is twice over, so take the 1 from the second. For 3, 7 is 4 plus 3.
guess it, thenthe move: a floor
the line:1 1 1 2 2 2 3 4 3 4 7 8 10 512 1,023 1,024 sits between half of and . Row 10 is section 7's count. why it is allowed: The bracket in line 7 is at least 0, as is at least 1. For 3, 7 is at least 4.
guess it, thenthe move: read the bound
the line:why it is allowed: By line 8. is half of , so the fixed multiple is a half. means at least a fixed multiple, above 0, from some on.
guess it, thenthe move: both together
the line:why it is allowed: Lines 6 and 9 bound by the same growth, , from above and below. By line 1, one more disc doubles the moves and adds 1.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: the start
the line:why it is allowed: One disc simply moves, once. So is right for 1 disc.
the move: the step
the line:why it is allowed: Take any from 2 up, and suppose the count is right for one disc fewer. For 3 discs, suppose 2 discs take 3 moves, which is 4 less 1.
guess it, thenthe move: use the rule
the line:why it is allowed: By question 1's line 1, discs take twice the moves for , plus 1. Put in line 2's count. For 3, 2 times 3, plus 1.
guess it, thenthe move: multiply out
the line:why it is allowed: Twice is , and twice 1 is 2. For 3, 8 less 2, plus 1.
guess it, thenthe move: tidy
the line:discs from the one before 1 the start, 1 1 2 2 times 1, plus 1, is 3 3 3 2 times 3, plus 1, is 7 7 4 2 times 7, plus 1, is 15 15 The start, then the step again and again. Each row's count agrees with , so the next row's does too. why it is allowed: Less 2, plus 1, is less 1. For 3, 7, so the count is right for 3 discs too.
guess it, thenthe move: every n
the line:why it is allowed: Line 1 starts it at 1 disc. Lines 2 to 5 carry it from any number of discs to one more. So it holds at 2, then 3, and on, for every .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: the biggest must move
the line:why it is allowed: It starts on A and must end on C, and only a move takes it there.
the move: before its first move
the line:peg discs from the bottom up A 3 B 2, 1 C none 3 discs, just before disc 3 first moves, at move 4. Discs 1 and 2 wait on B, the only peg left. why it is allowed: To move, it must be on top, so no disc is on it. No smaller disc may be where it lands, either. So every smaller disc is on the spare peg, the one it neither leaves nor lands on.
guess it, thenthe move: so a smaller tower moved
the line:why it is allowed: Moving discs from A to one other peg is the same puzzle, one disc smaller, with the pegs renamed. The biggest disc never gets in the way, as any disc may sit on it.
guess it, thenthe move: after its last move
the line:why it is allowed: When it last moves, onto C, the others are all on the spare peg again, for line 2's reason. Moving them from there onto C is the smaller puzzle again.
guess it, thenthe move: add them
the line:why it is allowed: Lines 3 and 4 count the moves before the biggest disc first moves and after it last moves. Line 1 adds at least 1 between them. No move is counted twice.
guess it, thenthe move: the same climb
the line:discs the fewest is at least why 1 1 line 1 2 3 2 times 1, plus 1 3 7 2 times 3, plus 1 4 15 2 times 7, plus 1 Each row is at least the row above doubled, plus 1: the same climb as question 2's. why it is allowed: Start: 1 disc needs at least 1 move, by line 1. Step: if discs need at least , line 5 gives at least . That is , by question 2's lines 4 and 5. So, as in question 2's line 6, it holds for every .
guess it, thenthe move: the fewest
the line:why it is allowed: By line 6 no way takes fewer, and question 1's method takes exactly . So the method takes the fewest moves there can be. For 3 discs, 7.
guess it, then
try it with a small : at , line 7 gives , which you can work out by hand
count it yourself
3 discs, step by step. The steps that only say which part comes next move no disc, so they add nothing.
| step | the row | what happens | discs moved |
|---|---|---|---|
| 1 | A: 3, 2, 1 | Move all 3 discs from peg A to peg C, one disc at a time, never putting a disc on a smaller one. Peg B is there to help.A: 3, 2, 1 | 0 |
| 2 | A: 3, 2, 1 | To move 3 discs from A to C, first move the 2 above the biggest out of the way, onto B. That is a smaller tower, moved the same way.A: 3, 2, 1 | 0 |
| 3 | A: 3, 2; C: 1 | Move disc 1 from A to C.A: 3, 2; C: 1 | 1 |
| 4 | A: 3; B: 2; C: 1 | Move disc 2 from A to B.A: 3; B: 2; C: 1 | 2 |
| 5 | A: 3; B: 2, 1 | Move disc 1 from C to B.A: 3; B: 2, 1 | 3 |
| 6 | B: 2, 1; C: 3 | Move disc 3 from A to C: it is where it belongs, and it will not move again.B: 2, 1; C: 3 | 4 |
| 7 | B: 2, 1; C: 3 | The biggest of those 3 is on C. Now move the 2 on B onto it, using A to help, the same way again.B: 2, 1; C: 3 | 4 |
| 8 | A: 1; B: 2; C: 3 | Move disc 1 from B to A.A: 1; B: 2; C: 3 | 5 |
| 9 | A: 1; C: 3, 2 | Move disc 2 from B to C: it is where it belongs, and it will not move again.A: 1; C: 3, 2 | 6 |
| 10 | C: 3, 2, 1 | Move disc 1 from A to C: it is where it belongs, and it will not move again.C: 3, 2, 1 | 7 |
| 11 | C: 3, 2, 1 | All 3 discs are on C after 7 moves. That is twice the moves for one disc fewer, and one more. No way takes fewer.C: 3, 2, 1 | 7 |
In all, 7 discs moved. The formula from section 4, , gives at : the same.
where you meet it
Tower of Hanoi is a puzzle from 1883, and its moves hide a pattern found in counting.
Edouard Lucas
The French mathematician Edouard Lucas invented the puzzle. In 1883 he put it out under a made-up name, Claus: shuffle its letters and they spell Lucas.
source: the MacTutor History of Mathematics, Edouard Lucas (opens in a new tab)
the ruler function
Number the moves 1, 2, 3 and on, and halve each move's number while it stays a whole number. Each move takes disc 1 plus its number of halvings: move 4 halves twice, to 2 and then 1, so it takes disc 3. These numbers are called the ruler function.
source: the On-Line Encyclopedia of Integer Sequences, A001511 (opens in a new tab)
| move | halvings | disc |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 1 | 2 |
| 3 | 0 | 1 |
| 4 | 2 | 3 |
| 5 | 0 | 1 |
| 6 | 1 | 2 |
| 7 | 0 | 1 |
what is used instead
To work the moves out with no calls waiting, use the smallest disc's rule. On odd moves, move disc 1 one peg along. It goes A to C to B and round for an odd number of discs, and A to B to C for an even one. For 3 discs, moves 1, 3, 5 and 7 take disc 1 from A to C, C to B, B to A and A to C. Section 1's table shows them. On even moves, make the only move that leaves disc 1 alone. It makes the same moves.
how it compares
The method uses itself on a smaller tower, and each call waits while its smaller tower moves, as the first table shows. The smallest disc's rule in section 6 makes the very same moves with nothing waiting. A fourth peg changes the puzzle. J. S. Frame and B. M. Stewart's way, from 1941, moves most of the discs aside using all four pegs, then the few biggest across on the three pegs left, then the rest back on top.
| call | its part | at move 1 |
|---|---|---|
| for 3 discs | A to C | waiting |
| for 2 discs | A to B | waiting |
| for 1 disc | A to C | moving disc 1 now |
| way | 10 discs | 20 discs |
|---|---|---|
| this method | 1,023 moves | 1,048,575 moves |
| the smallest disc's rule | 1,023 moves | 1,048,575 moves |
| four pegs, Frame and Stewart's way | 49 moves | 289 moves |
| discs | aside on four pegs | biggest on three pegs | moves |
|---|---|---|---|
| 10 | 6 discs, 17 moves there and 17 back | 4 discs, 15 moves | 17 + 15 + 17 = 49 |
| 20 | 14 discs, 113 moves there and 113 back | 6 discs, 63 moves | 113 + 63 + 113 = 289 |