Towers of Hanoi solution

Share this page
December 2006

Hanoi towers

The Towers of Hanoi

The Towers of Hanoi

The Towers of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883. It consists of three pegs and a number of discs of decreasing sizes. Initially, all discs sit on the same peg in the order of their size, with the biggest disc at the bottom. The aim is to move the whole tower of discs onto another peg, subject to the following rules:

  • when you remove a disc from a peg you must place it on one of the other two pegs before you can move any other disc,
  • you can only place a disc on a peg if either the peg is empty or if the discs already sitting there are larger than the disc you're about to place.

If you've only got two discs in total, then the puzzle is easy: move the top disc from peg 1 to peg 2, then move the bottom disc from peg 1 to peg 3, and finally move the smaller disc on peg 2 onto the larger disc on peg 3 — done. A little thought will show you that moving a tower of three discs is possible also. But can you show that it is possible to move a tower consisting of any number of discs?

Hint: Suppose that you have n discs in total. We've already seen that the puzzle can be solved for n equal to 2 and 3. Now assume that you can move a tower of n-1 discs: does this help you to move a tower of n discs? If yes, then why does this prove that the puzzle can be solved for any number of discs? (A proof that uses this way of thinking is an example of the principle of induction.)

Your proof now gives you a recipe for solving the puzzle for any number of discs. Write mn for the number of moves you need to solve the puzzle with n discs according to this recipe. Can you express mn in terms of mn-1? Can you show that mn is in fact the minimum number of moves needed to solve the puzzle with n discs, in other words that there is no quicker way?

The solution

Our first task is to prove that it is possible to move a tower of any height (that is one made up of any number of discs) from one peg to another without breaking the rules. We've already seen that it's possible to move a tower of height one, two or three. Now imagine that we have a tower of height n and that we know how to solve the game for a tower of height n-1. Our tower sits on peg 1, so that pegs 2 and 3 are empty.

Now we can move the top n-1 discs to peg 2 without ever placing a larger one onto a smaller one: we simply use the same method as we would if there were only n-1 discs in total. The fact that the bottom disc keeps sitting on peg 1 all the way through makes no difference, as it is bigger than all the others anyway, so we can place any disc we want on top of it.

Once the top n-1 discs are sitting on peg 2, we move the largest disc to peg 3 and then transfer the n-1-tower from peg 2 to peg 3 in the same way as before — we've moved the whole tower of height n!

What this argument tells us is that once we know how to move a tower of height n-1, we can move a tower of height n. So, since we know how to move the tower of height 2 — that was easy — we now have a way of moving a tower of height 3, then of height 4, and then height 5, etc, etc. This chain goes on forever and so any tower, no matter how tall, can be moved without breaking the rules.

Now for the second part of the question. The proof just described gives a recipe for moving a tower of height n. Write mn for the number of moves we need to do this. How does mn depend on mn-1? Well, moving the n-tower consisted of moving the n-1-tower twice plus one extra move. So mn = 2mn-1+1.

But is there any other way moving an n-tower; one that requires less moves? Let's suppose that there is. However this quicker method works, there's one move that you always have to make: you have to move the last disc at the bottom of peg 1 to another peg. So our new method can only be quicker if there is a quicker way to move the top n-1 discs. By the same argument, the new method is only quicker if there is a quicker way to move n-2 discs, and then n-3 and so on, all the way down to 2. So — there can only be a quicker method if there's a quicker way to move a tower of two discs than the one we described above. And it's pretty clear that there isn't. So no quicker method can possibly exist.



Back to main puzzle page