Home Segments Top Top Previous Next

442: Mainline

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: