Data Structures, Algorithms, & Applications in Java
Chapter 1, Exercise 25

(a)
f(5) = f(3 * 5 + 1) = f(16) = 16/2 = 8.
f(7) = f(3 * 7 + 1) = f(22) = 22/2 = 11.

(b)
The base component is f(n) = n/2, n is even and the recursive component is f(n) = f(3n + 1), n is odd.

When n is odd, one application of the recursive component transforms the occurrence of n on the right hand side to the base component because 3n + 1 is even whenever n is odd.

(c)
The recursive method misc.F.recursiveF is given below.


public static int recursiveF(int n)
   {return (n % 2 == 0) ? n / 2 : recursiveF(3 * n + 1);}




(d)
The iterative method misc.F.iterativeF is given below.


public static int iterativeF(int n)
   {return (n % 2 == 0) ? n / 2 : (3 * n + 1) / 2;}