# Towers of Hanoi

Jump to: navigation, search
Towers of Hanoi
The Towers of Hanoi is a well known puzzle game publicized in 1883 by Édouard Lucas, a French mathematician. According to legend, priests of the Hindu god Brahma were instructed to move 64 golden disks from one of 3 poles to another, and when they completed it the world would end.[1]

# Basic Description

The standard game begins with 3 poles, one of which (the starting pole) has some natural number, k, disks placed around it in increasing size from top to bottom, while the other poles start out empty.
Goal: Move all k disks from the starting pole (typically the leftmost) to the specified end pole (typically the rightmost), following these rules:
• Only one disk may be moved at one time.
• Each move consists of moving the top disk of one pole onto the top of another pole.
• Each disk must be placed on a larger disk.

The game is solvable and has a shortest solution (fewest moves) for any number of disks. To fully understand the game's legend, one must understand the relationship between the number of disks and the number of moves. As it turns out, this relationship is exponential (see A More Mathematical Section for details).

We can see the effects of this exponential relationship in the legend about the priests, who had to move 64 disks from one pole to the other. If they were to move one disk per second, then they would need 18,446,744,073,709,551,615 seconds, or 584.54 billion years, to complete the game. Given that this is about 58.5 times the life span of the Sun, the legend probably overestimates the amount of time we have until the world ends (unless we can survive without the Sun and the Earth).

### Play the Game

You can play the game Towers of Hanoi right here! This applet will help you understand how the game works.

If you can see this message, you do not have the Java software required to view the applet.

### Example of Solving the Game

Solving Towers of Hanoi with 4 disks.

# A More Mathematical Explanation

## The Recursive Solution

This game becomes more interesting when we try to figure out the shortest [...]

## The Recursive Solution

This game becomes more interesting when we try to figure out the shortest solution for any number of disks. We will first state an algorithm that solves the game, then show that this algorithm is the shortest solution. We will refer to the start pole as Pole A, the end pole as Pole C, and the remaining pole as Pole B. Define Tk to be the number of moves our algorithm needs to solve the game for k disks. To solve the game with 1 disk, simply move the disk from Pole A to Pole C, so

Image 1: Note that this case must occur for the kth disk to move freely onto Pole C.
$T_1=1$.

Assume that we know how to move k -1 disks (so we also know the value of Tk-1). To solve the game with k disks, follow these steps:

1. Move the top k -1 disks on Pole A to Pole B using our algorithm, taking Tk-1 moves.
2. Move the kth disk to Pole C, taking 1 move.
3. Move the k -1 disks on Pole B on top of the kth disk on Pole C using our algorithm, taking Tk-1 moves.

Hence we can describe the number of moves in our algorithm by the recursive formula:

$T_k=T_{k-1}+1+T_{k-1}=2T_{k-1}+1$
Image 2: After taking 1 step to move the kth disk onto Pole C, all that is left to do is move the k -1 disks onto Pole C.

We will now show by induction that our algorithm uses the smallest number of moves. The base case (T1 = 1) is satisfied because there is no way to solve the game without moving any disks. Assume that our algorithm takes the least number of moves to solve for k -1 disks, so Tk-1 is minimal.

To solve the game with k disks, the kth disk must be moved at some point, and moving it requires the top k -1 disks to be on the pole that is not the starting or ending pole of the kth disk. Since our algorithm needs the least number of moves to solve for k -1 disks, we will assume that moving the top k -1 disks takes Tk-1 moves. Note that there are two possible ways to move the kth disk: onto Pole B or onto Pole C. We will reason that moving it onto Pole C is the correct choice.

If we wish to move the kth disk onto Pole B, we must first move the top k -1 disks onto Pole C. We then need at least one additional move to move the kth disk onto Pole B. However, the end pole is Pole C, so the kth disk must be on Pole C before the remaining k -1 disks can be stacked on top of it. In order to move the kth disk to Pole C, we move the k -1 disks onto Pole A. Once again, we need at least one additional move to move the kth disk onto Pole C. Finally, we complete the game by moving the top k -1 disks onto Pole C. Using this approach, we had to move the kth disk at least twice and the k -1 disks three times.

If we wish to move the kth disk directly to Pole C, we must first move the top k -1 disks onto Pole B (see Image 1). We then need at least one additional move to move the kth disk onto Pole C. Finally, we complete the game by moving the top k -1 disks onto Pole C (see Image 2). Using this approach, we had to move the kth disk once and the k -1 disks twice, which is less than the previous approach. Since the rules imply that all moves are independent, we can conclude that this approach takes the least number of moves to solve for k disks.

Thus the minimum number of moves required to solve for k disks assuming Tk-1 is minimized is

$T_{k-1}+1+T_{k-1}=2T_{k-1}+1=T_k$

### The Explicit Formula

We will show by induction that the explicit formula for the number of moves in this minimal algorithm is

$T_k=2^k-1$, where k is the number of disks.

The base case is satisfied:

$T_1=2^1-1=2-1=1$ as desired.

Assume the (k -1)th case holds. Then

\begin{align} T_k & = 2T_{k-1}+1 \\ & = 2(2^{k-1}-1)+1 \\ & = 2(2^{k-1})-2+1 \\ & = 2^k-1 \\ \end{align}

Thus we have shown the minimal algorithm for any Towers of Hanoi game with k disks requires Tk = 2k – 1 steps.

## Back to the Legend

Now that we have the equation to determine the number of moves needed for any number of disks, we can verify the time we said was needed to complete the game in the legend. A Towers of Hanoi game with 64 disks will take:

$T_{64}=2^{64}-1=18,446,744,073,709,551,615\text{ moves}$

If the priests move 1 disk per second, then they will need:

$18,446,744,073,709,551,615\text{ s }\cdot \left(\frac{1\text{ min}}{60\text{ s}}\right)\left(\frac{1\text{ hr}}{60\text{ min}}\right)\left(\frac{1\text{ day}}{24\text{ hr}}\right)\left(\frac{1\text{ yr}}{365.25\text{ days}}\right)\approx584.54\text{ billion years}$

Assuming that the Sun has a life span of 10 billion years[2], the time predicted by the legend is $585.54\div10\approx58.5$ times longer than the Sun's lifespan.

# Why It's Interesting

Towers of Hanoi is a popular example in introductory computer science courses because programming it is instructional but relatively simple. The mathematical nature of the game teaches recursion[3], while the simple interface of the game introduces basic graphics processing. In addition, the game's exponential solution is an example of why computer scientists dislike exponential time; it takes far too long to compute. Assuming the average computer can do 100 million operations per minute, then solving the game with 64 disks would take the computer 5845.42 years to complete.

Towers of Hanoi is also a popular tool in psychological research; it can test problem solving capabilities.[4] Consequently, the game is often used to test intelligence. In the 2011 film Rise of the Planet of the Apes, scientists test the intelligence of apes with Towers of Hanoi.[5] The faster the apes solve the game, the more intelligent they are perceived to be.

# Teaching Materials

There are currently no teaching materials for this page. Add teaching materials.

Xintong Tian

# References

If you are able, please consider adding to or editing this page!

Have questions about the image or the explanations on this page?
Leave a message on the discussion page by clicking the 'discussion' tab at the top of this image page.