f(5) = f(3 * 5 + 1) = f(16) = 16/2 = 8.
f(7) = f(3 * 7 + 1) = f(22) = 22/2 = 11.
f(n) = n/2, n is even
and the recursive component is
f(n) = f(3n + 1), n is odd.
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.
misc.F.recursiveF
is given below.
public static int recursiveF(int n)
{return (n % 2 == 0) ? n / 2 : recursiveF(3 * n + 1);}
misc.F.iterativeF
is given below.
public static int iterativeF(int n)
{return (n % 2 == 0) ? n / 2 : (3 * n + 1) / 2;}