Then, move the biggest disk from start tower to final tower. The recursive code is usually smaller, more concise, more elegant, possibly even easier to understand, though that depends on ones thinking style. How will the monks solve this problem?
Another way to look at it is this, if we had a function to move the top three disks to the middle position, we could put the biggest disk in its place. If we do this the stack frame can be freed up. See advantages of tail recursion Head Recursion: A call is head-recursive when the first statement of the function is the recursive call.
A recursive function calls itself, the memory for a called function is allocated on top of memory allocated to calling function and different copy of local variables is created for each function call. Hint: Recursively find the factorial of the smaller numbers first, i.
What is the difference between direct and indirect recursion? It can be thought of like the Russian dolls that always have a smaller doll inside.
On the other hand, you might notice that the recursive versions use fewer or no local variables.
The computer keeps function calls on a stack and once too many are called without ending, the program will crash. The idea is to represent a problem in terms of one or more smaller problems, and add one or more base conditions that stop the recursion.