Tower of Hanoi

DOWNLOAD Mathematica Notebook TowersOfHanoiSolution of the three-rod four-disk towers of Hanoi problem

The tower of Hanoi (commonly also known as the "towers of Hanoi"), is a puzzle invented by E. Lucas in 1883. It is also known as the Tower of Brahma puzzle and appeared as an intelligence test for apes in the film Rise of the Planet of the Apes (2011) under the name "Lucas Tower."

Given a stack of n disks arranged from largest on the bottom to smallest on top placed on a rod, together with two empty rods, the towers of Hanoi puzzle asks for the minimum number of moves required to move the stack from one rod to another, where moves are allowed only if they place smaller disks on top of larger disks. The puzzle with n=4 pegs and n disks is sometimes known as Reve's puzzle.

The problem is isomorphic to finding a Hamiltonian path on an n-hypercube (Gardner 1957, 1959).

TowersofHanoiSolution

Given three rods and n disks, the sequence S_1={a_k} giving the number of the disk (i=1 to n) to be moved at the kth step is given by the remarkably simple recursive procedure of starting with the list S_1={1} for a single disk, and recursively computing

 S_n={S_(n-1),n,S_(n-1)}.
(1)

For the first few values of n, this gives the sequences shown in the following table. A solution of the three-rod four-disk problem is illustrated above.

nS_n
11
21, 2, 1
31, 2, 1, 3, 1, 2, 1
41, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1

As the number of disks is increases (again for three rods), an infinite sequence is obtained, the first few terms of which are illustrated in the table above (OEIS A001511). Amazingly, this is exactly the binary carry sequence plus one. Even more amazingly, the number of disks moved after the kth step is the same as the element which needs to be added or deleted in the kth addend of the Ryser formula (Gardner 1988, Vardi 1991). A simple method for hand-solving uses disks painted with alternating colors. No two disks of the same color are ever placed atop each other, and no disk is moved twice in a row (P. Tokarczuk, pers. comm. Jun. 23, 2004).

As a result of the above procedure, the number of moves h_n required to solve the puzzle of n disks on three rods is given by the recurrence relation

 h_n=2h_(n-1)+1
(2)

with h_1=1. Solving gives

 h_n=2^n-1,
(3)

i.e., the Mersenne numbers.

For three rods, the proof that the above solution is minimal can be achieved using the Lucas correspondence which relates Pascal's triangle to the Hanoi graph. While algorithms are known for transferring disks on four rods, none has been proved minimal.

A Hanoi graph can be constructed whose graph vertices correspond to legal configurations of n towers of Hanoi, where the graph vertices are adjacent if the corresponding configurations can be obtained by a legal move. The puzzle itself can be solved using a binary Gray code.

Poole (1994) and Rangel-Mondragón give Wolfram Language routines for solving the Hanoi towers problem. Poole's algorithm works for an arbitrary disk configuration, and provides the solution in the fewest possible moves.

The minimal numbers of moves required to order n=1, 2, ... disk on four rods are given by 1, 3, 5, 9, 13, 17, 25, 33, ... (OEIS A007664). It is conjectured that this sequence is given by the recurrence

 s_n=s_(n-1)+2^x,
(4)

with s_1=1 and x the positive floor integer solution to

 n-1=1/2x(x+1),
(5)

i.e.,

 x=|_(sqrt(8n-7)-1)/2_|.
(6)

This would then given the explicit formula

 s_n=1+[n-1/2x(x-1)-1]2^x.
(7)

Wolfram Web Resources

Mathematica »

The #1 tool for creating Demonstrations and anything technical.

Wolfram|Alpha »

Explore anything with the first computational knowledge engine.

Wolfram Demonstrations Project »

Explore thousands of free applications across science, mathematics, engineering, technology, business, art, finance, social sciences, and more.

Computerbasedmath.org »

Join the initiative for modernizing math education.

Online Integral Calculator »

Solve integrals with Wolfram|Alpha.

Step-by-step Solutions »

Walk through homework problems step-by-step from beginning to end. Hints help you try the next step on your own.

Wolfram Problem Generator »

Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.

Wolfram Education Portal »

Collection of teaching and learning tools built by Wolfram education experts: dynamic textbook, lesson plans, widgets, interactive Demonstrations, and more.

Wolfram Language »

Knowledge-based programming for everyone.