Let's see the case of 5 disks :

6,5,4,3,2, : : 1, :

6,5,4,3, : : 1, : 2,

6,5,4, : 3, : 1, : 2,

6,5,4, : 3,2, : 1, :

6,5,4, : 3,2,1, : :

6,5, : 3,2,1, : : 4,

6, : 3,2,1, : 5, : 4,

6, : 3,2,1, : 5,4, :

: 3,2,1, : 5,4, : 6,

4, : 3,2,1, : 5, : 6,

4, : 3,2,1, : : 6,5,

: 3,2,1, : : 6,5,4,

: 3,2, : 1, : 6,5,4,

2, : 3, : 1, : 6,5,4,

2, : : 1, : 6,5,4,3,

: : 1, : 6,5,4,3,2,

: : : 6,5,4,3,2,1,

17 Moves

If we look at step 8 above (in blue), we will see that the program has distributed top 5 disks as 3 : 2 in the middle 2 towers. It has then moved the 6th disk to 4th tower. It could have distributed them as 4 : 1, but has found that it will be more costly. Mini tower 3-2-1 it has created on tower2 was created using all 4 towers but the Mini tower it has created on tower3 (which has disks 4 and 5) was created using only 3 towers while keeping the already buildup content at tower 2 intact.

Once the biggest disk (disk 6) was moved to tower 4, program has created a mini tower 4-5 at top of tower4 while keeping tower 2 intact. After that step it has created a mini tower 3-2-1 on tower4 using all 4 towers.

Let's have a good look at again and make sure we understand the process!

In general, to solve 4 tower case for n disks, we need to follow these steps :

**1) find optimal numbers for x and y such that x+y=n-1**

**2) build a tower of hight x in tower2 using all 4 towers**

**3) build a tower of height y (starting from disk x+1) in tower3 while using only 3 towers (since the content we built on tower2 need to be preserved)**

**4) move the largest disk (which is n) from tower 1 to tower4**

**5) build a tower of height y on top of tower4 (starting from disk x+1) while using only 3 towers and keeping the content on tower2 intact.**

**6) build a tower of height x in tower4 using all 4 towers.**

Above is a recursive algorithm because to build a tower of n using all 4 towers, we will have to build a tower of x using all 4 towers!

Now, only one problem remaining.

How to decide on the best distribution between tower2 and 3 (in the example it was 3:2 and not 4:1), that is x was 3 and y was 2 how to find x and y ?

Here too we could use recursion!

*Following recursive algorithm and the program is something I have come up with and haven't seen in anywhere else yet. If any of you find a similar algorithm or a better one to solve this puzzle please inform me by putting a comment here with your finding. The code for this will appear in the next post*

We need some notations here to make things clearer :

Let T4(n) = 'steps to make a tower of height n using 4 towers'

Let T3(n) = 'steps to make a tower of height n using 3 towers'

Then we can express the steps needed for above 6 steps in this form :

**T4(n) = 2*(T4(x)+T3(y)) +1**

For instance, T4(6) = 2* (T4(3)+T3(2)) + 1

but we know T3(m) is 2

^{m}-1, hence T3(2) above is 2

^{2}-1 = 3

So,

**T4(6) = 2*T4(3)+7**

To build a tower of height 1, we need just 1 step.

According to above algorithm (steps 1 to 6 given above), building a tower of 2 will need 3 steps, since n=2, x=1 and y=0 in this case. (you can work out how we arrived at number 3 as home work!)

Now that we know steps needed to build a tower of 1 and a tower of 2, can we find the steps needed to create a tower of 3 ?

We have 2 choices

n =3, x=1,y=1 and

n=3, x=2, y=0

In the first case, steps needed will be 5. (home work for readers) In the second, it will be 7! (more home work) So we go with option 1. That means T4(3) =5 !!

Now we could revisit the problem of T4(6).

Earlier we found

**T4(6) = 2 * T4(3) + 7**

then T4(6) = 2*5 + 7 = 17 Which is exactly the number of steps our program has taken to solve!

So we could recursively find best

**x**and

**y**for any

**n**

All we need now is to write the nice recursive code. We will give the readers few days to come up with that code before unraveling it in our last post of Towers Hanoi series.

After that we will move to a new game and a new problem !

Your feedback on the Hanoi Towers series so far will be greatly welcome!