how to get exponents without using the math.pow for java

79,903

Solution 1

Powers of 2 can simply be computed by Bit Shift Operators

int exponent = ...
int powerOf2 = 1 << exponent;

Even for the more general form, you should not compute an exponent by "multiplying n times". Instead, you could do Exponentiation by squaring

Solution 2

You could implement your own power function.

The complexity of the power function depends on your requirements and constraints. For example, you may constraint exponents to be only positive integer.

Here's an example of power function:

public static double power(double base, int exponent) {
    double ans = 1;
    if (exponent != 0) {
        int absExponent = exponent > 0 ? exponent : (-1) * exponent;
        for (int i = 1; i <= absExponent; i++) {
            ans *= base;
        }

        if (exponent < 0) {
            // For negative exponent, must invert
            ans = 1.0 / ans;
        }
    } else {
        // exponent is 0
        ans = 1;
    }

    return ans;
}

Solution 3

You can try to do this based on this explanation:

 public double myPow(double x, int n) {

    if(n < 0) {
        if(n == Integer.MIN_VALUE) {
            n = (n+1)*(-1);
            return 1.0/(myPow(x*x, n));
        }
        n = n*(-1);
        return (double)1.0/myPow(x, n);
    }
    double y = 1;
    while(n > 0) {
        if(n%2 == 0) {
           x = x*x; 
        }
        else {
            y = y*x;
            x = x*x;
        }
         n = n/2;
    }
    return y;   
}

Solution 4

If there are no performance constraints you can do:

double x1=1;

for(int i=1;i<=numPowersOf2;i++){
   x1 =* 2
}

Solution 5

Here is a post that allows both negative/positive power calculations.

https://stackoverflow.com/a/23003962/3538289

Function to handle +/- exponents with O(log(n)) complexity.

double power(double x, int n){
 if(n==0)
  return 1;

  if(n<0){
      x = 1.0/x;
      n = -n;
  }
 double ret = power(x,n/2);
 ret = ret * ret;
 if(n%2!=0)
   ret = ret * x;
 return ret;
}
Share:
79,903
Admin
Author by

Admin

Updated on January 19, 2020

Comments

  • Admin
    Admin over 4 years

    This is my program

    // ************************************************************
        // PowersOf2.java
        //
        // Print out as many powers of 2 as the user requests
        //
        // ************************************************************
    
        import java.util.Scanner;
    
        public class PowersOf2 {
    
        public static void main(String[] args)
    
        {
            int numPowersOf2; //How many powers of 2 to compute
            int nextPowerOf2 = 1; //Current power of 2
            int exponent= 1;
            double x;
    
             //Exponent for current power of 2 -- this
            //also serves as a counter for the loop Scanner
    
            Scanner scan = new Scanner(System.in);
            System.out.println("How many powers of 2 would you like printed?");
            numPowersOf2 = scan.nextInt();
            System.out.println ("There will be " + numPowersOf2 + " powers of 2 printed");
            //initialize exponent -- the first thing printed is 2 to the what?
    
        while( exponent <= numPowersOf2)
            {
            double x1 = Math.pow(2, exponent);
            System.out.println("2^" + exponent + " = " + x1);
                    exponent++;
            }   
    //print out current power of 2
    //find next power of 2 -- how do you get this from the last one?
    //increment exponent
    
        }
    }
    

    The thing is that I am not allowed to use the math.pow method, I need to find another way to get the correct answer in the while loop.

  • Pritam Banerjee
    Pritam Banerjee about 5 years
    There are performance constraints on this one. Will be O(n). It can be done in O(1) time.