You are on page 1of 5

The Towers of Hanoi

1 The Towers of Hanoi


The Towers of Hanoi is an interesting recursion based puzzle. The invention of this puzzle
is credited to French mathematician Édouard Lucas in the 19th century. The Towers of
Hanoi are often associated with the legend of a Hindu temple where the puzzle was used
to develop the mental discipline of young priests. In the legend the priests were given
64 golden discs stacked on one of three posts with the largest one at the base and then
ascending in size order. The priests goal was to re-create the stack by moving the discs,
one at a time, to another post with the rule that smaller discs cannot be placed on larger
ones. Variations of this legend say that the completion of this task will signal the end of
the universe!
Since most people don’t have access to 64 golden discs the puzzle is traditionally set up
as in the picture below.

The best solution to the puzzle is the solution which uses as few moves as possible. At
first glance it is not obvious that this puzzle can be solved at all. You can find a nice
online version of the puzzle at http://haubergs.com/hanoi (There are also many free
versions of the puzzle available on both the Google Play store and the Apple App store
or you can make a Towers of Hanoi puzzle with little more than cardboard and scissors).
In order to get a handle on the recursive nature of this puzzle let’s start small.

• If there is only one disc then we only need one move.

Step 0

Step 1
1
• If there are two discs then we need three moves.

Step 0

Step 1

Step 2

Step 3

• If there are three discs then we need seven moves.

Step 0

Step 1

Step 2

Step 3

Step 4

2
Step 5

Step 6

Step 7

What about a tower with four discs? Analysis of the three above solutions yields a clue.
When solving the puzzle with three discs the solution is to transfer the top two discs to
the middle peg, then move the third, and finally transfer the two pegs onto it. In other
words, top solve the problem with three discs we

• Move the tower of two discs to the middle peg - 3 moves.

• Move the third peg - 1 move.

• Move the tower of two discs onto the third disc - 3 moves.

This gives us a clue as to how to solve the problem with any number, n, of discs. We first
move the tower of n − 1 discs to the middle peg, then move the nth disc and then move
the tower with n − 1 discs on top of the nth disc. If Tn is the number of moves required
to solve the puzzle with n discs then

Tn = Tn−1 + 1 + Tn−1
= 2Tn−1 + 1

This recurrence relation describes the number of moves needed to solve the Towers of
Hanoi puzzle with n discs. We’d now like to solve this recurrence relation, that is, we’d
like to find a general formula for the number of moves required to solve the puzzle with
n discs which doesn’t require us to solve the puzzle on n − 1 discs first. To get a feel for
a possible solution we’ll make a table of the first few values of Tn .

3
n Tn
0 0
1 2(0) + 1 = 1
2 2(1) + 1 = 3
3 2(3) + 1 = 7
4 2(7) + 1 = 15
5 2(15) + 1 = 31
6 2(31) + 1 = 63
7 2(63) + 1 = 127
8 2(127) + 1 = 255
It looks as if the values of Tn are one less than 2n . In other words it would appear that
Tn = 2n − 1.
Theorem 1
The general solution to the recurrence relation Tn = 2Tn−1 + 1 is given by

Tn = 2n − 1.

Proof

The proof proceeds by induction. If your students are unfamiliar with this technique
or it is beyond their current level you may wish to exclude this section.

• For the base case n = 0 we have

T0 = 20 − 1 = 1 − 1 = 0.

• Suppose the statement is true for n = k i.e.

Tk = 2k − 1.

• Now for n = k + 1 we have

Tk+1 = 2Tk + 1
= 2(2k − 1) + 1
= 2k+1 − 2 + 1
= 2k+1 − 1

Thus we have proven Theorem 1 and can say for certain that

Tn = 2n − 1.

Using this we can calculate how many moves it takes to complete a tower with any
number of discs.

4
Relating this to the legend of the Hindu priests let us use what we have found to see just
how long the universe has left.

Example
Assume that it takes 2s to move a single disc. How long will it take to solve a
Tower of Hanoi puzzle with 64 discs?
Solution:
First we need to calculate the number of moves. Using Theorem 1

Tn = 2n − 1
T64 = 264 − 1
= 18 446 744 070 000 000 000 moves

Now that we have the number of moves we convert this into a time.

18 446 744 070 000 000 000 × 2 = 36 893 488 140 000 000 000 s
= 614 891 469 000 000 000 min
= 10 248 191 150 000 000 hr
= 427 007 964 600 000 days
= 1 169 884 834 000 years

Thankfully then it will take the priests approximately 1.2 trillion years to finish a
Tower of Hanoi with 64 discs. So we have some time left yet.

The Towers of Hanoi is an excellent puzzle that is not only fun to solve but is also a great
way to introduce recurrence relations.

You might also like