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, begin with a stack of 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
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 -hypercube (Gardner 1957, 1959).
Given three rods and
disks, the sequence gives the label of the disk ( to ) moved at the th step. Starting with , it is defined for by concatenating, the one-element list, and , written as
(1)
For the first few values of , this gives the sequences shown in the following table. A
solution of the three-rod four-disk problem is illustrated above.
1
1
2
1, 2, 1
3
1, 2, 1, 3, 1, 2, 1
4
1, 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 th
step is also the element added or deleted at the th 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 required to solve the puzzle of disks on three rods is given by the recurrence
relation
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 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 , 2, ... disks on four rods are given by 1, 3, 5, 9, 13,
17, 25, 33, ... (OEIS A007664). For , this sequence satisfies
the recurrence relation
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 -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 Stevin21, 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." Quantum6, 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. Monthly48,
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 Haard22, 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. Monthly46, 363, 1939.Stewart,
B. M. "Solution to Problem 3918." Amer. Math. Monthly48,
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.