In the great temple of Kashi Vishwanath, so the legend goes, priests tend a brass plate fitted with three rods.
On the first rod rest 64 discs of pure diamond, each one smaller than the one beneath it, the largest at the bottom.
Day and night the priests move the discs, one at a time, from the first rod to the third. The legend says that when the very last disc is placed, the universe will come to an end.
So how long do the priests actually need? To find out, we'll start with a much smaller tower 👇
The brass plate has three rods: Rod 1, Rod 2, and Rod 3.
Rod 1 holds the tower of discs, largest at the bottom, smallest at the top.
The mission: move the entire tower from Rod 1 to Rod 3.
The priests may use Rod 2 along the way, but only as a temporary resting place.
Before the priests face 64 discs, let's try it with just 3.
Click a rod to pick up its top disc, then click another rod to place it there.
Take a minute to look back at what you just did. There are no right answers here, just think each one over in your own words.
Let's walk back through the moves you made, one idea at a time, starting with the smallest tower possible.
Start with the simplest tower there is: a single disc. Nothing sits on top of it and nothing is in the way, so you just pick it up and put it on Rod 3. One move, no planning.
When a problem gets so small that you can just do it, that's the base case. It's the point where all the thinking stops.
Now put a bigger disc underneath. You can't touch the big one while the small one is sitting on it, so the small disc has to get out of the way first. That makes 3 moves: small disc aside, big disc across, small disc back on top.
You probably felt this one in your hands. To free the biggest disc, you had to move the top two discs onto Rod 2. And that's exactly the 2-disc puzzle you just saw, only with the rods playing different roles. After the big disc crosses, you solve that same 2-disc puzzle again to bring them over.
n−1 discs, then 1 big move, then n−1 discs again
That plan works for any tower. Notice that the two “move n−1 discs” jobs are the very same problem you started with, just smaller. Handing that smaller tower to the same set of steps is called a recursive call, and the whole “n−1 twice, plus one big move” plan is the recursive case.
While you were parking the top discs on Rod 2, the job “move the biggest disc” hadn't happened yet. It was on hold, waiting for the smaller job to finish. A program keeps track of those paused jobs in a call stack. Here's the 3-disc case:
Once the smallest job is finished, it drops off the stack, and the job underneath it wakes up and carries on from exactly where it paused. That step back is called returning.
# move n discs from one rod to another def move(n, start, end, spare): if n == 1: print("move disc 1:", start, "->", end) # base case return move(n - 1, start, spare, end) # clear the way print("move disc", n, ":", start, "->", end) # the one big move move(n - 1, spare, end, start) # bring them back on top
Let T(n) be the fewest moves needed for n discs. From the plan above, we make two trips with n−1 discs, plus one move for the biggest disc:
Let's use it on small towers first:
| Discs (n) | Working | T(n) | 2n |
|---|---|---|---|
| 1 | base case | 1 | 2 |
| 2 | 2 × 1 + 1 | 3 | 4 |
| 3 | 2 × 3 + 1 | 7 | 8 |
| 4 | 2 × 7 + 1 | 15 | 16 |
| 5 | 2 × 15 + 1 | 31 | 32 |
Spot it? Every answer is exactly one less than a power of 2. Here's why that always works:
With the formula in hand, let's go back to the priests of Kashi Vishwanath. Slide the number of discs up and watch how fast the numbers run away.
That is 0% of Brahma's entire lifespan (311 trillion years).
🕉️ How long does Brahma live?
🛕 So, what does the legend say? With 64 discs the priests need 264 − 1 moves, about 1.84 × 1019 (roughly 18.4 quintillion). Even at one move every second, that's over 580 billion years. It sounds endless, but Brahma's life is about 311 trillion years, so the priests' whole task is just a tiny slice of it. By the Hindu cosmic timeline, the present Brahma's life is far from over, which means the end of the universe is still very, very far away.
🏛️ A small note on the story. In the West this puzzle is called the Tower of Hanoi. It's the way of solving a big problem by handing a smaller copy of it to the same steps.
Seven questions to think through at your own pace. There is no single right answer, so take your time.