Solve one of the following three recursive problems 1 Ackerm
Solve one of the following three recursive problems:
1. Ackermann\'s function is a recursively-defined function where computing a value requires computation of a simpler case of the function. It is formally defined as a function on two values A(m, n), as follows:
A(m, n) = n + 1 if m = 0
A(m - 1, 1) if m > 0 and n = 0
A(m - 1, A(m, n - 1)) if m > 0 and n > 0
Write a Java function to compute Ackermann\'s function given m and n. Note: the results get too large to compute with even small values of n and m. Try A(2, 3) = 9 or A(3, 2) = 29.
or
2. Write a recursive method that will calculate and return the range of values in an array. The range is defined as the maximum value in the array minus the minimum value. Hint: you will need a different approach than the solution to problem 2, because you need to calculate both the minimum and maximum values, and a method can have only one return value.
or
3. Write a recursive method that will find whether or not a particular substring occurs inside another (like indexOf except with a boolean return value). For example, searching \"water\" for \"ate\" returns true, but searching \"water\" for \"war\" returns false. Here\'s the catch: you can only use the String charAt and length methods . Also, be careful, because if you search \"array\" for \"ra\", the answer is true (don\'t get caught up looking only at the first \"r\"). If you like you can have one recursive method that calls another recursive method. Your solutions must be completely recursive, and have no loops. If your recursive method requires additional parameters not specified by the problem, write a helper method that will call the recursive method with correct initial parameters. Submit your complete recursive method (and any helper methods required) by the due date specified in the course schedule.
Solution
Solution1>
Ackermann\'s recursively-defined function :
public static int A(int m, int n) {
if (m < 0 || n < 0) {
throw new IllegalArgumentException(\"Non-negative args only!\");
}
if (m == 0) {
return n + 1;
} else if (n == 0) {
return A(m-1, 1);
} else {
// perforce (m > 0) && (n > 0)
return A(m-1, A(m,n-1));
}
}
