If there is one disc, then we move 1 disc from the source pole to the destination pole. Otherwise, we move n - 1 discs from the source pole to the temporary pole, we move 1 disc from the source pole to the destination pole, and we finish by moving the n - 1 discs from the temporary pole to the destination pole.

void TOH(int n, int p1, int p2, int p3) { if (n == 1) printf("Move top disc from %d to %d.\n", p1, p2); else { TOH(n-1, p1, p3, p2); printf("Move top disc from %d to %d.\n", p1, p2); TOH(n-1, p3, p2, p1); } }

Of course, we can simplify this to the following:

void TOH(int n, int p1, int p2, int p3) { if (n>1) TOH(n-1, p1, p3, p2); printf("Move top disc from %d to %d.\n", p1, p2); if (n>1) TOH(n-1, p3, p2, p1); }

Pretty cool, huh? This example shows the power of recursion to turn what seems like a hard and intricate problem into something much more simple that can be solved in three lines of code.

Actually, the whole story of the monks is just a legend. In fact, it isn't even an old legend. The story was created in 1883 by a mathematician named Edouard Lucas. He had invented an eight disc, three tower puzzle, and created the legend in order to sell his product.

That being said, what if the story were true? Should we be worried about the world ending when the monks solve the puzzle? After all, they live in the 21st century, too, and have access to the same information about recursion that we have.

Luckily, just as mathematics helps us solve the puzzle, it also helps prove that our grandchildren will still have a world to live in. In order to figure out how long it will take the monks to solve the puzzle, we need to write a recurrence relation, a recursive formula for describing the size of a recursive problem. Let's call our recursive formula T(n), where n is the number of discs.

As seen above, the base case for the Towers of Hanoi problem is when n is 1. This is when the monks just have to move one disc from one pole to another. So T(1) = 1. For the recursive case where n! = 1, we need a more complicated formula. The monks, in the recursive case, follow a three step procedure. They move n - 1 discs, then they move 1 disc, and then they move n - 1 discs. So T(n) = T(n - 1) + T(1) + T(n - 1) = 2T(n - 1) + 1.

Now we know that to solve a Towers problem with n discs takes T(n) = 2T(n - 1) + 1 steps. It would be nice if we had a closed-form solution to this recurrence so that we could figure out exactly how long it will take. A closed form solution is a formula without recursion, meaning we can simply plug in numbers and get our answer.

Let's plug in some sizes and solve the Towers problem with those sizes to see if we can find a closed-form solution.

  • T(1) = 1
  • T(2) = 3
  • T(3) = 7
  • T(4) = 15
  • T(5) = 31
  • T(6) = 63
  • T(7) = 127
  • T(8) = 255
  • T(9) = 511
  • T(10) = 1023
Notice a pattern here? T(n) = 2n - 1. To prove to yourself that this is true, try modifying your TOH code to count the number of disc moves. Create a global variable count, run your modified TOH code, and then print out count.

Now we can easily compute how long it would take the monks to solve their 64-disc Towers problem. 264 - 1 is approximately 18.45x1018 (note that if you actually tried to run the TOH code on your computer it would most likely take a very, very long time). If the monks could move a disc in a millisecond (an incredibly rate considering the size and weight of each disc), it would take them approximately 584,600,000 years to solve the puzzle. It appears the world is safe for now.