0

I have a problem with my homework and i need help please!

Question 1:

Complete the Java methods below so that raiseToPower(x,n) will raise the number x to an integer power n (that is, to calculate the value xn ). Remember that x-n = 1/xn, and that x0 = 1.

You should do this in the fewest number of steps possible (that is, in O(log n) time).

Give a solution which is non-recursive (iterative):

This is my solution :

    public static double raiseToPower (double x, int n) {
double res=1;
     boolean neg=false;
     if(n<0)
     {
         neg=true;

     }
     if(n>0)

         for (int i=0;i<n;i++) {
             res = res * x;
         }
             if(neg==true) {
                 n=n*-1;
                 for (int i=0;i<n;i++) {


                     res = res * x;
                 }
                   res=1/res;
             }


             return res;
}

but this is not correct because is not efficiency

This my error for example: 52.49 to the power of 9 solved in 9 steps, but it could have been done in 4 steps 89.89 to the power of 75 solved in 75 steps, but it could have been done in 7 steps 78.57 to the power of 63 solved in 63 steps, but it could have been done in 6 steps 70.17 to the power of 44 solved in 44 steps, but it could have been done in 6 steps

Note:must not be used in method java.lang.MathPow

Question 2:

I need write code exactly like Question 1 but in recursive

This is my Question: Give a recursive solution:

This is my code:

     public static double raiseToPower (double x, int n) {
ouble dev=0.0;
        if (n == 0) {
            return 1;
        } else {
            if (n < 0) {
              double  count= raiseToPower (x, n+1);
                dev=count*x;
                return 1 / raiseToPower (x, -n);
            }
            if (n > 0) {
             double  count= raiseToPower (x, n-1);
             dev=count*x;



            }

        }
        return dev;
}

This code is correct but not efficiency.

This is my error for example:

53.31 to the power of 44 solved in 44 steps, but it could have been done in 6 steps 6.90 to the power of 74 solved in 74 steps, but it could have been done in 7 steps 80.76 to the power of 76 solved in 76 steps, but it could have been done in 7 steps 51.44 to the power of 86 solved in 86 steps, but it could have been done in 7 steps 76.26 to the power of 50 solved in 50 steps, but it could have been done in 6 steps 63.53 to the power of 93 solved in 93 steps, but it could have been done in 7 steps

Note:must not be used in method java.lang.MathPow

Thank you everyone for helping and solving both problems !!!

2 Answers2

1

You can calculate in O(logN) x^n by breaking down n in 2's powers, like so:

9 = 1+8

15= 1+2+4+8

Therefore, x^9= (x^1)*(x^8).

In order to break down n in 2's powers, you can use bitwise operators. Like this: n&pow2 would mean you do the "AND" operation between N and pow2, which means if n has a bit 1 and pow2 also has that bit 1, the result will be non-zero. Given that pow2 must have a single bit 1( it is a power of 2), you can basically check every bit of n. So you are breaking down n in the powers of 2 and you can simply keep a powx around that means x^(pow2) as you are looping through the powers of 2, then multiply it to res whenever you find that n indeed is composed of that power of 2.

So we can make this code for the first solution:

  public static double raiseToPower (double x, int n) {
    double res=1;
    double powx=x;
    int pow2=1;
    boolean neg=false;
    if(n<0)
    {
      neg=true;
      n=n*-1;
    }
    while(n!=0) {
      if((n&pow2)!=0)
      {
        res=res*powx;
        n=n-pow2;
      }
      powx=powx*powx;
      pow2=pow2*2;
    }
    if(neg==true)
      res=1/res;
    return res;
  }

Here's more articles about bitwise operators: https://www.tutorialspoint.com/java/java_basic_operators.htm

Similarly, you can modify the recursive code to get it in O(logN).

Here would be the recursive code:


  public static double raiseToPower(double x, int n)
{
    boolean neg= false;
    double res=1;
    if(n<0)
    {
      neg=true;
      n=-n;
    }

    if (n == 0) return 1;
    if (n % 2 == 0)
    {
      res= raiseToPower(x, n / 2);
      res=res*res;
    }
    else
    {
      res= x * raiseToPower(x, n - 1);
    }
    if(!neg)
      return res;
    return 1/res;
}
JunisvaultCo
  • 151
  • 7
0
public class ExponentialCalculator {

    public static void main(String[] args) {
        double x = 2;
        int n = -4;
        System.out.println(raiseToPower(x, n));
    }

    //Divide and Conquer method
    public static double raiseToPower (double x, int n) {
        if(n==0) {
            return 1;
        }
        double temp = raiseToPower(x, n/2) * raiseToPower(x, n/2);
        if(n%2==0) {
            return n > 0 ? temp: 1/temp;    
        }
        else {
            return n > 0 ? x * temp: 1/(x * temp);
        }
    }
}


result 0.0625

Complexity Log(n)

Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37