![]() |
![]() |
![]() |
![]() |
![]() |
|
Next, note that you can arrange for recursivePowerOf2 to hand
over a little less work to powerOf2 by performing one of the
multiplications by 2 in recursivePowerOf2 itself, and subtracting
1 from powerOf2's argument:
public static int recursivePowerOf2 (int n) {
if (n == 0) {return 1;}
else {
return 2 * powerOf2(n - 1);
}
}
Clearly, recursivePowerOf2 must work as long as one of the
following two situations holds:
n, is 0; in this
situation, recursivePowerOf2 returns 1.
n is not 0, but powerOf2
is able to compute the power of 2 that is 1 less than the
value of n.