# The Solution to the Towers of Hanoi

Created | Updated Jul 28, 2003

### The Problem

The towers of Hanoi starts with three pegs, with different sized discs on the left peg (as shown below):

There are two rules for moving the discs:

- Only one disc may be moved at once.

A disc may only be moved onto an empty peg, or onto a larger sized disc (i.e. a larger disc cannot be placed on top of a smaller one).

The aim is to move the whole tower from the left peg onto the right peg in the smallest possible number of moves. The number of discs can vary, but the greater the number of discs the more moves are required to complete the puzzle, and the more difficult the puzzle becomes.

This is the solution to the problem when there are three discs:

**7 Moves**

The fewest number of moves in which the puzzle with three discs can be completed is seven moves.

The problem therefore is this:

puzzle can be completed for a given number of discs?

### The Solution

The solution to this problem can be found by beginning with the simplest case, with just one disc:

To complete the puzzle in the fewest number of moves, the disc can simply be moved from the left peg to the right peg. |

**1 Move**

The solution to the puzzle with one disc is one move. This can now be used to solve the puzzle for two discs:

To complete the puzzle in the fewest number of moves, the small disc must be moved onto the middle peg (to allow the bottom disc to be moved onto the right peg). This first move is the same as the solution to the puzzle with one disc, but this time the disc is being moved to a different peg. |

The next move is to move the large disc to the right peg. |

The puzzle can now be completed by moving the small disc onto the right peg. This move is again the same as the solution to the puzzle with one disc, but from a different peg. |

**3 Moves**

The solution to the puzzle with two discs is three moves. For the puzzle with two discs to be completed, the move to complete the puzzle with one disc must be made to move the small disc onto the centre peg, followed by a move of the large disc onto the right peg, followed again by the move to complete the puzzle with one disc to move the small disc onto the right peg. In other words, to complete the puzzle with two discs, the move to complete the puzzle with one disc must be made twice, plus an extra move (to move the large disc).

This principle is the same for larger numbers of discs. For the puzzle with

**n**discs to by completed, the combination of moves that are used to complete the puzzle with

**n - 1**discs must be made to move all of the discs except the largest one onto the centre peg. Then a single move of the largest disc onto the right peg can be made. To complete the puzzle, the combination of moves to complete the puzzle with

**n - 1**discs must be made to move all of the discs from the centre peg onto the right peg. Therefore, the number of moves which it takes to complete the puzzle with

**n**discs is twice the number of moves it takes to complete the puzzle with

**n - 1**discs plus 1. If the number of moves to complete the puzzle with

**n**discs is written as

**m**then:

_{n}**m**

_{n}= 2m_{n - 1}+ 1Or written the other way around:

**m**

_{n}= 1 + 2m_{n - 1}Using this equation, the following must also be true:

**m**

_{n - 1}= 1 + 2m_{n - 2}This can be substituted into the original equation to give:

**m**

_{n}= 1 + 2( 1 + 2m_{n - 2})Multipltying out the brackets gives:

**m**

_{n}= 1 + 2 + 4m_{n - 2}Again, using the original equation, the following must be true:

**m**

_{n - 2}= 1 + 2m_{n - 3}This can be substituted into the equation above, and after multiplying gives:

**m**

_{n}= 1 + 2 + 4 + 8m_{n - 3}These substitutions can be continued. It can be seen that this is a sum of successive powers

^{1}of 2. Therefore this equation can be rewritten as:

**m**

_{n}= 2^{0}+ 2^{1}+ 2^{2}+ 2^{3}m_{n - 3}The substitutions can be continued until the term

**m**is reached. Since the coefficient

_{1}^{2}of the term

**m**can be seen to be

_{n - a}**2**, the coefficient of the term

^{a}**m**(which is the same as

_{1}**m**) must be

_{n - (n - 1)}**2**. Therefore, after further expansions the following equation can be written:

^{n - 1}**m**

_{n}= 2^{0}+ 2^{1}+ 2^{2}+ 2^{3}+ 2^{4}+ … + 2^{n - 1}m_{1}**m**is known to be 1 (since it takes one move to complete the puzzle with one disc). Therefore:

_{1}**m**

_{n}= 2^{0}+ 2^{1}+ 2^{2}+ 2^{3}+ 2^{4}+ … + 2^{n - 1}This equation says that

**m**is equal to a sum of consecutive powers of two, from the power

_{n}**2**to the power

^{0}**2**. This summation can be proved

^{n - 1}^{3}to be equal to

**2**. Therefore, the solution is:

^{n}- 1**m**

_{n}= 2^{n}- 1Therefore the fewest number of moves in which the puzzle with

**n**discs can be completed is

**2**moves.

^{n}- 1^{1}2 to the power of n means 1 times 2 n times. For example, 2 to the power 4 equals 1 x 2 x 2 x 2 x 2 and is written as 2 ^ 4 or 2

^{4}.

^{2}Multiple of. The coefficient of

**8m**is 8.

_{n - 3}^{3}Proof by induction can be used to prove this.