Originally Posted by
Norm
Thanks for posting your solution. I see that the code passes 3 variables to the recursive method.
Yea,also a little typo from me.
public class test {
public static void main(String[] args) {
System.out.println(isPower_recursive(2,16));
}
public static boolean isPower(int x, int n) {
if (x == 1)//header
return (n == 1);//header
int pow = 1;//header
while (pow < n) {//loop
pow = pow * x;// loop body
}
return (pow == n); //return statement
}
public static boolean isPower_recursive(int x,int n) {
if(x == 1)
return (n==1);
return isPower_recursion( x, n, 1);
}
public static boolean isPower_recursion(int x,int n, int pow) {
if( pow >= n) {
return (pow == n);
}
pow = pow * x;
return isPower_recursion(x,n,pow);
}
}
Its >= not >.