# Tower of Hanoi

The **Tower of Hanoi** (also called **Towers of Hanoi**) is a mathematical game or puzzle. It consists of three pegs, and a number of discs of different sizes which can slot onto any peg. The puzzle starts with the discs neatly stacked in order of size on one peg, smallest at the top, thus making a conical shape.

The object of the game is to move the entire stack to another peg, obeying the following rules:

- only one disc may be moved at a time
- a disc can only be placed onto a larger disc (it doesn't have to be the adjacent size, though: the smallest disc may sit directly on the largest disc)

Table of contents |

2 How to solve it 3 Applications 4 External Links |

## Origins

The puzzle was invented by the French mathematician Edouard Lucas in 1883. There is a legend about a Hindu temple whose priests were constantly engaged in moving a set of 64 discs according to the rules of the Tower of Hanoi puzzle. According to the legend, the world would end when the priests finished their work. The puzzle is therefore also known as the Tower of Brahma puzzle. It is not clear whether Lucas invented this legend or was inspired by it.

Interestingly, if the legend were true, and if the priests were able to move discs at a rate of 1 per second using the smallest number of moves, it would take the priests 2^{64} - 1 or roughly 1.8×10^{19} seconds to finish the puzzle. This is equivalent to about 580,000,000,000 years, which is more than the estimated age of the universe.

In the classic science fiction story "Now Inhale", by Eric Frank Russel
(*Astounding Science Fiction* April 1939, and in various anthologies),
the human hero is a prisoner on a planet where local custom is to
make the prisoner play a game until it is won or lost, and then
execution is immediate. The hero is told, the game can be one of
your own species, so long as it can be played in your cell with
simple apparatus, strictly according to rules you write down before
play begins and cannot change after play starts, and has a
finite endpoint. The game and execution are televised planet-wide,
and watching the desperate prisoner try to spin the game out as long
as possible is a very popular entertainment; the record is sixteen days.
The hero knows a rescue ship might take a year
or more to arrive, so chooses to play Towers of Hanoi with
64 disks until rescue arrives. When the locals realize they've
been had, they are angry, but under their own rules there is
nothing they can do about it. They do change the rules that
will apply to any *future* prisoners.

## How to solve it

Most toy versions of the puzzle have 8 discs. The game seems impossible to the novice, yet is solvable with a simple algorithm:

### Recursive Algorithm

- label the pegs A, B, C -- these labels may move at different steps
- let
*n*be the total number of discs - number the discs from 1 (smallest, topmost) to
*n*(largest, bottommost)

- move n-1 discs from A to C. This leaves disc #n alone on peg A
- move disc #n from A to B
- move n-1 discs from C to B so they sit on disc #n

*n*-1. The entire procedure is a finite number of steps, since at some point the algorithm will be required for

*n*= 1. This step, moving a single disc from peg A to peg B, is trivial.

#### Explanation of the Algorithm

A more human-readable version of the same algorithm follows:

You now have 2 discs stacked correctly on peg C, peg B is empty again- move disc 3 to peg B
- repeat the first 3 steps above to move 1 & 2 to sit on top of 3

*i*up to 1, move disc

*i+1*from peg A, the starting stack, and move the working tower again.

### Practical Algorithm

The Tower of Hanoi is a problem often used to teach beginning programming. A pictorial version of this puzzle is programmed into the emacs editor, accessed by typing M-x hanoi.

## Applications

The Tower of Hanoi is frequently used in psychological research on problem solving. There also exists a variant of this task called Tower of London for neuropsychological diagnosis and treatment of executive functions.