Encyclopedia > Tower of Hanoi

  Article Content

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)

Most toy versions of the puzzle have 8 discs. The game seems impossible to the novice, yet is solvable with a simple 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)

Table of contents

Recursive Algorithm

To move n discs from peg A to peg B:

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

The above is a recursive algorithm: to carry out steps 1 and 3, apply the algorithm again for 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:

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

Each time you re-build the tower from disc i up to 1, move disc i+1 from peg A, the starting stack, and move the working tower again.

Practical Algorithm

Alternately move disc 1 and one of the larger discs. If two of two of the larger discs are available, always move the smaller one onto the larger. When you move an odd-numbered disc, always move it one peg clockwise; when you move an even-numbered disc, always move it one peg counterclockwise.

In spite of the simplicity of the algorithms, the shortest way to solve the problem with n discs takes 2n-1 moves. It is not known in general how many moves are required to solve the puzzle when there are more than 3 pegs.

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.

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.

Two pages about the origins on the puzzle:



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Quadratic formula

... remain correct if the coefficients a, b and c are complex numbers, or more generally members of any field whose characteristic is not 2. (In a field of characteristic 2, ...

 
 
 
This page was created in 38.5 ms