
A psychological question on the side: Which size of towers can you move just by looking at the picture and for which size do you need to formalize `Divide` and `Build`? Or do you use another way of solving the puzzle? Does your method scale to even larger sizes? Can you write down an algorithm (in pseudocode) that works for any size of towers? (Ignore questions of efficiency for now, we will come back to this.) : Referring back to the note on [Recursion as a Problem Solving can you say what your methods for `Divide` and `Build` are? Write them down in English. **Activity:** Try to describe your algorithm.

If you feel confident solving the puzzle with 5 or 6 disks, you may have found an algorithm which works for any number `n` of disks. **Activity:** How does it work for 4 disks? What is the biggest number of disks you can do? For size `n=3`, where do you have to place the top disk in the first move? There is only one correct answer. So, the expression for the N discs will become, Moves N = (2 * Moves N-1) + 1 = 2 N - 1. The number of moves almost doubles every time you insert another disk in the stack.
Hanoi towers function series#

Moving N - 1 discs from tower C to tower B. Moving Nth disc from tower A to tower B.Ĭout<< "\t\tMove disc " << N << " from " << source <<" to "<< dest <<"\n" TowerofHanoi(N - 1, source, dest, inter) Moving N - 1 discs from tower A to tower C. void towerOfHanoi(int N, char source, char inter, char dest)Ĭout<< "\t\tMove Disc 1 from " << source << " to " << dest << "\n"

Hanoi towers function code#

Move the ‘Nth’ disc from tower A to tower B.Recursively, move top’ N – 1′ discs from tower A to tower C.Now let us derive the general solution for the ‘N’ number of discs. We can see that 7 moves will be required to shift 3 Discs from tower A to tower B with the help of an intermediary tower C.
