Jaap's Puzzle Page

Tower of Hanoi

Tower of Hanoi

This puzzle consists of three pegs, and a stack of circular disks of differing sizes, each of which can be threaded onto a peg. At the start, the disks are all in order on the first peg, from the largest disk at the bottom to the smallest disk at the top. You are only allowed to move one disk at a time from one peg to another, and at no time may a disk be placed on top a smaller disk. The aim of the puzzle is to move all the disks from the first peg to the third. The puzzle can be solved with any number of disks, but it takes longer with more disks.

This puzzle was invented by the French mathematician Edouard Lucas in 1883. The description included with the puzzle when it was originally sold called it a simplified version of a mythical 'Tower of Brahma' in a temple in the Indian city of Benares. This tower consisted of 64 disks of gold, which had to be moved to another position by priests. The legend went that before they complete the task, the temple would crumble into dust and the world would vanish in a clap of thunder.

If your browser supports JavaScript, then you can play the Towers of Hanoi by clicking the link below:

JavaScript Towers of Hanoi

The number of possible positions:

If the disks are placed on the pegs one by one, starting with the largest one, then there are always three choices when placing a disk. Any legal position can be constructed in this manner. There are therefore 3N positions for N disks. At each position (except when all the disks are on one peg) there are 3 possible moves, one which brings you closer to the start, one which brings you closer to the end, and one which brings you closer to having all disks on the middle peg.

Solution:

The puzzle can be solved by a recursive algorithm. To move a stack of N disks from one peg to another, simply follow the following steps:
  1. Move the top N-1 disks of the stack aside, on the spare peg.
  2. Move disk N in position on the other peg.
  3. Move the stack of N-1 disks on top of disk N.

This method works because this reduces the puzzle of N disks to a puzzle of only N-1 disks. That in turn is reduced to a puzzle of N-2 disks, and so on until the puzzle is reduced to moving a stack containing only 1 disk, which is easy. This algorithm has exactly 2N-1 moves, because from the steps above it is easily seen that Length(N)=2Length(N-1)+1, and Length(1)=1, and the result then follows by induction. This solution is also minimal, in other words there is no shorter way to solve it, because every solution for N disks must reach the intermediate position with the top N-1 disks on the spare peg.

The recursive algorithm works very well, but in practice there are several rules of thumb which can guide you:

There are 3N possible positions, and 2N positions are encountered while solving the puzzle directly using the methods above. All the other 3N-2N positions break the odd/even rules by having two disks of the same parity on top of each other (or the largest disk on the middle peg). The odd/even rule always rules out one of the three available moves, so that you can only go forwards to the end or back to the beginning.

If you stop in the middle of solving and forget what your last move was, then you may find yourself reversing previous moves and ending up back at the beginning. The rules of thumb also do not help you when you encounter one of the 3N-2N positions that do not lie on the direct route between the start and the end. In these cases the correct move can be deduced as follows:

  1. You want to move the stack of N disks to the third peg. You therefore want to put disk N on the third peg first.
  2. If the disk is in position, then the aim is to put all the smaller disks on top of it. If not, then the aim is to put all the smaller disks on the other peg.
  3. Repeat the above for this smaller stack of disks.

This is best illustrated by example:

Example: A:1, B:4/2, C:3

Here we want to move disk 4 from peg B to peg C. This means that we need all the smaller disks to be on peg A, so we must move disk 3 from peg C to peg A first. For this we need all disks smaller than 3 to be on peg B. Disk 2 is already on peg B, but disk 1 is not so this has to be moved to peg B. The correct move is therefore disk 1 from A to B.
Similarly if we want to move back to start, to move disk 4 from B to A means that disk 3 and smaller must be on peg C. Disk 3 is already on C, so we must put disk 2 on C next. Disk 1 does not obstruct this, so the correct move is disk 2 from B to C.
If we wanted all the disks on peg B instead, then the correct move would be move disk 1 from A to C. Note that this move does break the odd/even rule (disk 1 moves on top of disk 3), but that is only because the position itself already breaks that rule (disk 2 lies on disk 4).

An interesting pattern emerges if you map out all possible positions in a graph, with lines connecting two positions that differ by a single move. The graph will resemble of Sierpinski's gasket; the more disks, the more closely its graph approximates the gasket's fractal triangular shape. The solution is the path along the edge of the triangle, from one corner to the other.

Size 4 graph, Sierpinski