Up: Unit 11 Previous: Moor: construction of the

## Exercises

Exercise 11.1 Provide an iterative implementation of a method that computes the n-th Fibonacci number.

Exercise 11.2 Modify the recursive implementation of the fibonacci() method in such a way that, when it is called on the integer n, it computes besides the n-th Fibonacci number, also the total number of recursive activations of fibonacci() used for the computation.

Exercise 11.3 Provide an implementation of the method that calculates the Ackermann function A(m, n), which is defined as follows:

A(m, n) =

Note that the Ackermann function grows very fast (it is a non-elementary function): A(x, x) grows faster than any tower of exponentials 22 ... 2x.

Exercise 11.4 Implement recursive methods that are based on the following inductive definitions:

• greatest common divisor:

gcd(x, y) =

• check whether two positive integers are relative prime:

prime(x, y) =

• rest of the division between an integer and a positive integer:

rest(x, y) =

Exercise 11.5 Provide the implementation of a recursive method that counts how many occurrences of 1 appear in a sequence of integers read from a file (accessed through a BufferedReader).

Exercise 11.6 Provide the implementation of a recursive method that takes as parameters a string s and a character c and returns the length of the longest sequence of consecutive occurrences of character c in s.

Exercise 11.7 Provide the implementation of the method toString() of the class Traversal in such a way that the returned string represents the whole moor, using: -

• the character 'o' for the water zones,
• the character '#' for the land zones that belong to the traversal, and
• the character '*' for the remaining land zones.

Exercise 11.8 In the implementation of the search of a traversal through a moor shown above, certain land zones could be visited several times. This makes the search for a traversal inefficient, in the worst case even exponential in the number of columns of the moor. Provide examples of a moor with a traversal and of a moor without a traversal for which the method searchTraversal(), as implemented above, is activated a number of times that is exponential in the number of columns of the moor. Modify the class Traversal and the implementation of the method searchTraversal() in such a way that zones of the moor that have already been visited are marked, and thus are not visited several times. Verify that, with the new implementation, the number of activations of searchTraversal() is proportional to (rather than exponential in) the number of zones of the moor.

Exercise 11.9 Modify the class Traversal in such a way that it returns a traversal through the moor in the case where we can move in all directions (as long as we stay on land zones). Note that, in this case, it is necessary to modify the representation of a traversal, since it could be longer than the number of columns of the moor, and hence could traverse different zones on the same column. Moreover, in this case, it becomes unavoidable to mark the land zones that have already been visited during the search for a traversal, in order to avoid infinite looping on the same zones.

Up: Unit 11 Previous: Moor: construction of the