Moves until the end of the world
Three pegs, and a stack of disks of different sizes piled on the left peg, biggest at the bottom. Your job is to move the whole stack to the right peg. Two rules: you move one disk at a time, and you may never put a bigger disk on a smaller one.
Three disks take 7 moves with a little thought. Four disks? Five? The puzzle comes with a legend: when monks finish moving 64 golden disks, the world will end. Relax. The story came printed with the puzzle when it went on sale, and there is no evidence that it is an old legend at all.
The puzzle was invented by the French mathematician Édouard Lucas. He had worked at the Paris Observatory, served as an artillery officer in the Franco-Prussian War, and then taught mathematics at Paris lycées, as French high schools are called. In 1883 he put the puzzle on sale as a game "brought back from Tonkin" by "Professor N. Claus de Siam, mandarin of the College of Li-Sou-Stian." Tonkin is today's northern Vietnam. The joke was hidden in plain sight: N. Claus de Siam rearranges the letters of "Lucas d'Amiens," and Li-Sou-Stian those of Saint-Louis, the lycée where he taught. The story of Brahmins at a temple in Benares moving 64 golden disks came in the same booklet. A mathematician's sense of fun became the advertising. Lucas also wrote a four-volume collection of recreational mathematics, Récréations mathématiques.
Besides, the world is safe for a while. Even at one move per second, 64 disks would take about 585 billion years, more than forty times the age of the universe, which is about 13.8 billion years.
Our questions: why does the number of moves blow up so fast? How can we be sure a solution is the shortest? And if the disks start scattered over all three pegs, how do we find the shortest way to gather them? Chasing the answers leads straight to recursion, an idea that every programmer uses every day.
Look at the biggest disk
Look at the biggest disk. At the moment it moves from left to right, where are all the others? They cannot be on the left peg: sitting on top of the big disk, they would block it. They cannot be on the right peg: the big disk would land on smaller ones. So at that instant, all the other disks must be stacked on the middle peg.
That pins down the plan. First move the top disks to the middle peg, then move the biggest disk to the right peg, and finally move the disks from the middle peg onto it. The first and last steps are the same problem again, just one disk smaller. A procedure that uses itself like this is called recursive.
How many moves, and why not fewer?
Let be the fewest moves for disks in the standard puzzle. The plan above gives
Add 1 to both sides: . Each extra disk doubles , so and : 1, 3, 7, 15, 31, 63, …
But is it really the fewest? "I tried and it took 7 moves" proves nothing about shortest. We have to show that no one, however clever or however sneaky, can do it in fewer. Here is the key. The biggest disk must move at least once. Just before its first move, the other disks must all be on the third peg, which takes at least moves. After its last move, those disks must be piled back on top: at least more. So any solution needs at least moves, and our plan uses exactly that. "This is enough" plus "nothing less can work," and by induction is the minimum.
If you study sequences in mathematics, is a textbook recurrence, and the proof that is the minimum is induction in action. The puzzle shows what recurrences and induction are for.
By the way, Lucas himself had a soft spot for numbers of the form . Primes of this form are called Mersenne primes, after the 17th-century French friar Marin Mersenne. In 1876 Lucas used a test of his own to prove that , a 39-digit number, is prime. It stayed the largest known prime for 75 years, until 1951, when a larger one was found with a desk calculating machine; electronic computers took over the hunt soon after. In 1930 Derrick Lehmer polished his method into the Lucas–Lehmer test, and the prime , found in 2024 with more than 41 million digits, was confirmed with exactly that test.
A procedure that asks itself for help
Thought experiment: you are handed a 5-disk tower. You ask a friend, "Please move this 4-disk tower to the middle." You move your big disk once. Then you ask again: "Now move the 4 disks to the right." Your friend does the same with a 3-disk tower, and so on. Who is waiting for whom? A computer keeps exactly this pile of unfinished requests; it is called the call stack. In the simulation, the right-hand side shows it live. Where the analogy fails: in the computer, every "friend" is you. The same procedure keeps asking itself for help, stacking up a note of "how far I had got" each time.
That pile of notes has its own history. In 1946 Alan Turing described calling and returning from subroutines as "burying" and "unburying." In 1955 Klaus Samelson and Friedrich Bauer in Munich proposed a container where you put things on top and take them off the top, which they called the Operationskeller, the "operation cellar," and filed a patent in 1957. We call it a stack. In 1960 Edsger Dijkstra was building a translator, or compiler, for ALGOL 60, a language that let procedures call themselves. In a paper titled "Recursive Programming," he showed how a stack makes recursion work in general. The call stack on the right of the simulation is that idea.
Today programmers use recursion daily. Think of searching a folder that contains folders that contain folders, evaluating an expression with brackets inside brackets, or merge sort, which sorts a list by splitting it in half again and again. Whenever a problem splits into smaller copies of itself, recursion fits. Even without recursion, the apps on your phone push a frame onto a call stack every time one procedure calls another, and pop it off when it returns. Recurse too deeply and the stack overflows; the error is literally called a stack overflow, and a famous question-and-answer site for programmers is named after it.
And the cost? The number of moves, , doubles with every disk: exponential growth. But the call stack is only about deep. The simulation produces moves one at a time as they are needed, so even for 64 disks the first move appears instantly. Writing out the whole list first would take until the end of the world. Even a computer producing a billion moves a second would need about 585 years for all 64 disks.
Exponential growth matters in security too. Every extra bit in a key doubles the number of candidates an attacker must try, so checking all keys is hopeless for the same reason that 64 disks never finish.
Keeping time with binary
A pretty pattern: in the standard solution, the disk moved on move is given by writing in binary and counting the zeros at the end, plus one. Move 1 moves disk 1, move 2 disk 2, move 3 disk 1, move 4 disk 3, and so on. In binary, 2 is and 4 is . The smallest disk moves every other time. Solving the Tower of Hanoi keeps the same rhythm as counting in binary! Each time you count up by one, exactly one digit turns from 0 to 1: the ones digit every 2 counts, the twos digit every 4, the fours digit every 8. Disk 1 moves every 2 moves, disk 2 every 4, disk 3 every 8. Spot the resemblance?
But why does it work? The -disk solution is "the -disk solution, one move of disk , the -disk solution again." So disk moves exactly in the middle, on move , and the two halves repeat the -disk solution. Apply the same reasoning to the smaller towers and you find that disk moves only on moves that are odd multiples of . And if is an odd multiple of , its binary form ends in exactly zeros. That is the whole secret of the rule.
Binary numbers turn up in more ways than one. is ones in binary, and the disk moved at step depends on the trailing zeros of . There is more: in the Gray code, which orders binary numbers so that only one digit changes at each step, the digit that changes at step follows exactly the same rule as the disk that moves on move . Gray codes are used in sensors that read the angle of a rotating shaft, to avoid misreadings when several digits would otherwise change at once. There is even a backup scheme known as "Tower of Hanoi" rotation, in which the backup media are reused in the same rhythm as the disks move.
Starting from a scattered arrangement
What if the disks start scattered? Same idea: look at the biggest disk. If it is already on the target peg, forget it; it is as good as the floor. If not, it should move once, straight to the target. For that, every smaller disk must first be gathered on the remaining peg, which is the same kind of problem, one size smaller.
The same reasoning gives a formula for gathering everything on peg . Go from the biggest disk down. If a disk is on the current goal peg, it costs nothing and the goal stays the same; if not, add moves, where is the disk's number counting from the smallest, and switch the goal for all smaller disks to the remaining peg. Why ? Once disk has made its one move to the goal, the smaller disks have to be stacked back on top of it, which is the standard puzzle and takes moves. Add the move of disk itself and you get . The work of first gathering the smaller disks on the remaining peg is counted further down, as a smaller problem with its goal switched. The total is the minimum. With seven scattered disks the simulation might show "16 + 8 + 2 = 26 moves". We checked this rule against a search of every arrangement for up to six disks, and it always agrees. Six disks alone have arrangements.
There is geometry hiding here as well: disks can be arranged in ways, and if you connect arrangements that are one move apart, the picture has the shape of the Sierpiński triangle, triangles nested inside triangles. Within this collection, Search Algorithms with the 8-Puzzle looks for a shortest path without trying everything, and Graph 3-Coloring backs up from dead ends. The Tower of Hanoi shows the third possibility: once you understand a problem's structure, you can build the shortest answer with no search at all.
Add a fourth peg, though, and the problem becomes hard: Frame and Stewart described a method in 1941, but a proof that it is optimal for four pegs came only in 2014, from Thierry Bousch. And in 1989 Andreas Hinz showed that when you go from one scattered arrangement to another by a shortest route, the largest disk that has to move moves either exactly once or exactly twice.
Solve it yourself
The puzzle turns up in psychology, too. In 1975 Herbert Simon analyzed the several different strategies people use on it. Later studies report that solvers often start with guided trial and error and end up with a recursive strategy: "to move this tower, first move the tower above it." In clinics and psychology labs, the Tower of Hanoi and its variant the Tower of London, devised by Tim Shallice in 1982, are used to test the ability to look ahead and plan. Which strategy will you end up with?
- Set Number of disks to 3. Drag the top disk to another peg, or tap it and then tap the destination peg. Can you finish in 7 moves while "Moves off the shortest path" on the right stays at 0?
- Predict first: how many moves for 5 disks? Write it down, set Number of disks to 5, and read "Shortest from the start" on the right.
- Press 🧩 Solve in fewest moves and watch "Recursive calls (call stack)" on the right grow and shrink. One move advances a single move, so you can see which step, ①, ② or ③, each call is on.
- Set Starting arrangement to "Small, medium, large on separate pegs". Press Merge from the smallest, then 🧩 Solve in fewest moves. Where did the merging method take a detour?
- Press Another random arrangement and work out the minimum yourself before reading the breakdown on the right.
- Set Number of disks (calculation only) to 64 and read the numbers. What about 30?
What a boxed puzzle teaches
Focusing on the biggest disk split an -disk problem into two -disk problems: recursion. The minimum for the standard puzzle is , and to call it the shortest we had to show both that it suffices and that nothing shorter can work. From a scattered start, thinking from the biggest disk down still gives the shortest moves, whereas the natural idea of merging disks from the smallest upward can take detours. The number of moves grows exponentially, but generating them one by one needs memory only about deep.
A boxed puzzle that a playful math teacher sold under an anagram became the textbook example of recurrences and induction, and the most famous showcase of recursion and the stack, the ideas underneath every program. To ponder: when moving from one scattered arrangement to another, moving the biggest disk twice is sometimes shorter. Can you find such a case?
Sources and further reading
The facts in this article were checked mainly against the following.
- MacTutor, "Édouard Lucas" (University of St Andrews): his career, in 1876, the Récréations mathématiques
- MacTutor, "The Towers of Hanoi" (University of St Andrews): the N. Claus de Siam and Li-Sou-Stian anagrams and the accompanying legend
- "Picture of the cover for the original Tower of Hanoi" (College of William & Mary): the 1883 box and its text
- GIMPS, "GIMPS Discovers Largest Known Prime Number: 2^136,279,841-1," 2024: confirmation with the Lucas–Lehmer test
- T. Bousch, "La quatrième tour de Hanoï," Bulletin of the Belgian Mathematical Society – Simon Stevin, 2014: the proof for four pegs
- E. W. Dijkstra, "Recursive Programming," Numerische Mathematik, 1960
- H. A. Simon, "The functional equivalence of problem solving skills," Cognitive Psychology, 1975
- T. Shallice, "Specific impairments of planning," Philosophical Transactions of the Royal Society B, 1982: the Tower of London task
Further reading (encyclopedias):
- Wikipedia, "Tower of Hanoi": Frame and Stewart, Hinz's result, Hanoi graphs and the Sierpiński triangle
- Wikipedia, "Stack (abstract data type)": Turing, Samelson and Bauer
- Wikipedia, "Largest known prime number": as the record for 75 years, and Ferrier's larger prime found with a desk calculator in 1951
- Wikipedia, "Gray code"
- Wikipedia, "Backup rotation scheme": "Tower of Hanoi" rotation