TOPICS
Search

Tower of Hanoi


TowersOfHanoi
Solution of the three-rod four-disk tower of Hanoi problem

The tower of Hanoi (commonly also known as the "towers of Hanoi") is a puzzle invented by E. Lucas in 1883 (Bousch 2014). 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."

For a positive integer n, begin with a stack of n disks arranged from largest on the bottom to smallest on top on one rod, together with two empty rods. The tower of Hanoi puzzle asks for the minimum number of moves required to move the stack to another rod. Each move transfers the top disk of one rod to an empty rod or onto a larger disk. The puzzle with four pegs and n disks is sometimes known as Reve's puzzle.

The sequence of disk labels in the standard optimal three-rod solution corresponds to the coordinates changed along the binary reflected Gray code, a Hamiltonian path on an n-hypercube (Gardner 1957, 1959).

TowersofHanoiSolution

Given three rods and n>=1 disks, the sequence S_n={a_k} gives the label of the disk (i=1 to n) moved at the kth step. Starting with S_1={1}, it is defined for n>=2 by concatenating S_(n-1), the one-element list {n}, and S_(n-1), written as

 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 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. The label of the disk moved at the kth step is also the element added or deleted at the kth transition of the binary reflected Gray code ordering of the subsets in the Ryser formula (Gardner 1988b, 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)

for n>=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. For four rods, Bousch (2014) proved that the algorithm of Frame (1941) and Stewart (1941) is minimal. Its optimality for five or more rods remains conjectural in general (Hinz and Parisse 2026).

A Hanoi graph can be constructed whose graph vertices correspond to legal configurations of n disks on three rods, 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 tower 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, ... disks on four rods are given by 1, 3, 5, 9, 13, 17, 25, 33, ... (OEIS A007664). For n>=2, this sequence satisfies the recurrence relation

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

with s_1=1. For n>=1, the parameter x=|_(sqrt(8n-7)-1)/2_| is the nonnegative integer satisfying 1/2x(x+1)<=n-1<1/2(x+1)(x+2), where |_·_| is the floor function. The explicit formula is

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

Wolfram (2022) analyzes the tower of Hanoi as a multicomputational process, including through the use of branchial graphs.


See also

Binary Carry Sequence, Gray Code, Hanoi Graph, Pancake Sorting, Puz-Graph, Ryser Formula

Explore with Wolfram|Alpha

References

Allouche, J.-P. "Note on the Cyclic Towers of Hanoi." Theor. Comput. Sci. 123, 3-7, 1994.Allouche, J.-P. and Shallit, J. "The Ring of k-Regular Sequences." Theoret. Comput. Sci. 98, 163-197, 1992.Bogomolny, A. "Towers of Hanoi." https://www.cut-the-knot.org/recurrence/hanoi.shtml.Bousch, T. "La quatrième tour de Hanoï." Bull. Belg. Math. Soc. Simon Stevin 21, 895-912, 2014. https://doi.org/10.36045/bbms/1420071861.Brousseau, A. "Tower of Hanoi with More Pegs." J. Recr. Math. 8, 169-176, 1975-1976.Chartrand, G. "The Tower of Hanoi Puzzle." §6.3 in Introductory Graph Theory. New York: Dover, pp. 135-139, 1985.Daiev, V. "Problem 636: Greatest Divisors of Even Integers." Math. Mag. 40, 164-165, 1967.Dubrovsky, V. "Nesting Puzzles, Part I: Moving Oriental Towers." Quantum 6, 53-57 (Jan.) and 49-51 (Feb.), 1996.Flajolet, P.; Raoult, J.-C.; and Vuillemin, J. "The Number of Registers Required for Evaluating Arithmetic Expressions." Theoret. Comput. Sci. 9, 99-125, 1979.Frame, J. S. "Solution to Problem 3918." Amer. Math. Monthly 48, 216-217, 1941.Gardner, M. "Mathematical Games: About the Remarkable Similarity between the Icosian Game and the Towers of Hanoi." Sci. Amer. 196, 150-156, May 1957.Gardner, M. "The Icosian Game and the Tower of Hanoi." Ch. 6 in The Scientific American Book of Mathematical Puzzles & Diversions. New York: Simon and Schuster, pp. 55-62, 1959. https://books.google.com/books?id=ErAyAAAAMAAJ.Gardner, M. "The Icosian Game and the Tower of Hanoi." Ch. 6 in Hexaflexagons and Other Mathematical Diversions: The First Scientific American Book of Puzzles and Games. Chicago, IL: University of Chicago Press, pp. 55-62, 1988a.Gardner, M. Time Travel and Other Mathematical Bewilderments. New York: W. H. Freeman, 1988b.Hinz, A. M. and Parisse, D. "From Lucas to Frame and Stewart." Fibonacci Quart. Published online Aug. 6, 2026. https://doi.org/10.1080/00150517.2026.2671204.Kai, Y. and Chuan, X. "Preliminary Probe of 4-Peg Hanoi Tower." Acta Scient. Natur. Univers. Pekin. 40, 99-106, 2004.Kasner, E. and Newman, J. R. Mathematics and the Imagination. Redmond, WA: Tempus Books, pp. 169-171, 1989.Kolar, M. "Tower of Hanoi (TH) Puzzle." http://HanoiTower.mkolar.org/.Kraitchik, M. "The Tower of Hanoi." §3.12.4 in Mathematical Recreations. New York: W. W. Norton, pp. 91-93, 1942.Lucas, É. Récréations mathématiques, Vol. 3. Paris, France: Gauthier-Villars, 1891-1894. Reprinted Albert Blanchard, 1960.Numberphile. "Key to the Tower of Hanoi." Oct. 27, 2021. https://www.youtube.com/watch?v=PGuRmqpr6Oo.Poole, D. G. "The Towers and Triangles of Professor Claus (or, Pascal Knows Hanoi)." Math. Mag. 67, 323-344, 1994. Rangel-Mondragón, J. "Generalized Hanoi Tower." https://library.wolfram.com/infocenter/MathSource/4861/.Ruskey, F. "Towers of Hanoi." https://web.archive.org/web/20140529080015/http://www.theory.csc.uvic.ca/~cos/inf/comb/SubsetInfo.html#Hanoi.Schoute, P. H. "De Ringen van Brahma." Eigen Haard 22, 274-276, 1884.Sloane, N. J. A. Sequences A001511/M0127, A007664/M2449, and A007665/M2414 in "The On-Line Encyclopedia of Integer Sequences."Stewart, B. M. "Problem 3918." Amer. Math. Monthly 46, 363, 1939.Stewart, B. M. "Solution to Problem 3918." Amer. Math. Monthly 48, 217-219, 1941.Vardi, I. Computational Recreations in Mathematica. Reading, MA: Addison-Wesley, pp. 111-112, 1991.Wolfram, S. "Games and Puzzles as Multicomputational Systems." Jun. 8, 2022. https://writings.stephenwolfram.com/2022/06/games-and-puzzles-as-multicomputational-systems/.Wood, D. "Towers of Brahma and Hanoi Revisited." J. Recr. Math. 14, 17-24, 1981.Yang, S. "Animate the Optimal Set of Moves for 4-Peg Tower of Hanoi." Oct. 31, 2024. https://community.wolfram.com/t/24733.

Referenced on Wolfram|Alpha

Tower of Hanoi

Cite this as:

Weisstein, Eric W. "Tower of Hanoi." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/TowerofHanoi.html

Subject classifications