1. How will the first recursive call to towers() be invoked? Answer this
question in the form: towers(x, y, z) where you give the actual values to the
three parameters.
2. How many recursive calls to towers() will be made before this first recursive
call actually returns to the initial invocation?
3. Once towers(5, 2, 3) has invoked its first recursive call to towers() and this
invocation has returned, what will be printed to stdout? (i.e. What actual
move will be made by towers(5, 2, 3) ?) 4. How will the second recursive call to towers() be invoked? Answer this
question in the form: towers(x, y, z) where you give the actual values to the
three parameters.
1. In the main program, towers() is a function. Therefore the recursive call to towers() will be invoked by placing values in the brackets. When printing out the program, the dotted lines represent the recursive calls. The recursive call is such
that "n = n-1", "from" remains the same, and "spare = 6-from-dest". Since towers(5,2,3) is the initial invocation, the first recursive call will be invoked with the following: towers(4,2,1)
2. 5 recursive calls to towers() will be made before the first recursive call actually returns to the initial invocation. Once the recursion goes to towers(0,2,1), the recursion goes back into the previous calls. Therefore, 5 recursion calls will be made!
3. Once towers(5, 2, 3) has invoked its first recursive call to towers() and this invocation has returned, the output will be the following:
2 3
This represents the disc moving from Tower 2 to Tower 3. 4. In the main program, towers() is a function. Therefore the recursive call to towers() will be invoked by placing values in the brackets. When printing out the program, the dotted lines represent the recursive calls. The recursive call is such
that "n = n-1", "from" remains the same, and "spare = 6-from-dest". Since towers(4,2,1) is the first recursive call, then the second recursive call will be invoked with the following: towers(3,2,3)
Question:
Suppose that towers(8, 1, 2) is invoked. How many lines will be printed to stdout? Note
• You should note (or try to convince yourself) that the number of lines printed to
stdout is precisely equal to the number of moves required to solve the problem.
• You can use the theoretical analysis of the problem to determine the number of moves.
• The towers command behaves somewhat differently than what has been described so far. In particular, if it is invoked with an argument, it will move the specified number of disks from Tower 1 to Tower 2. After printing the data in the terminal, there are 255 moves, therefore, there must
be 255 lines printed to stdout. Also from the formula, Moves = 2 ^n -1, where n is the number of disk. In this case 'n' is 8.
Moves = 2 ^8 -1 = 255
Therefore, 255 lines