I'm trying to complete an assignment for my Java class where we are to create an efficient method for solving powers (taking a base to an exponent, defined by user's input).
I have just about everything in place, but the efficient method pow2 isn't handling odd exponents correctly. It multiplies the base by one more than it's supposed to. What am I missing here?
For instance, if I input 4 as my base and 5 as my exp, the program will spit out 4096 instead of 1024.
import java.util.Scanner;
public class MyPow {
public static int countGbl = 0;
public static double raise(double base, int exp) {
double b = base;
for (int i = 1; i < exp; i++){
countGbl += 1;
base = base * b;
}
return base;
}
public static double pow1(double base, int exp) {
if (base == 0.0) return Double.POSITIVE_INFINITY;
else if (base == 0.0 && exp >= 0) return 0.0;
else if (exp == 1) return base;
else if (base > 0 && exp == 0) return 1.0;
else{
if (exp < 0){
base = 1.0/base;
exp = -exp;
}
if (exp % 2 == 0){
countGbl++;
return pow1(base, exp/2) * pow1(base, exp/2);
}
else {
countGbl += 2;
return pow1(base, exp/2) * pow1(base, exp/2) * base;
}
}
}
public static double pow2(double base, int exp) {
double temp = raise(base, exp/2);
double retval = temp*temp;
if (exp % 2 == 1){
countGbl++;
retval *= temp;
}
return retval;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter base: ");
double base = Integer.parseInt(s.nextLine());
System.out.println("Enter exp: ");
int exp = Integer.parseInt(s.nextLine());
System.out.println(pow2(base, exp));
System.out.println(countGbl);
}
}
Just an FYI, the reason for the pow1 method is because we are to write an example method that isn't as efficient as pow2.
Also, please ignore countGbl. Our professor wants us to count the number of multiplications done during the method calls.
raise()andpow2()methods. Just callpow1(base,exp)frommain(). It will return count of multiplications as 19 mind. Then you can add memoization to pow1() to limit multiplications.