-2

I am trying to write a function in java that finds the result of an operand raised to the power of another.

I can't use the pow function or any form of loop. What are any possible solutions? I tried "^" and that didn't work.

public static String raiseP(int op1, int op2){
    int result  = op1 ^ op2;  //Doesn't Work
    return result; 
}

Would there be a way to do this using basic math?

I have written:

public static int pow(int x, int y, int n, int z){
  if (y == n){
      System.out.println(z);
      return z;
  }
  else{
      z = z*x;
      n += 1;
      pow(x,y,n,z);
      return 0;      
  }

}
ex: pow(5,9,0,1) == 5^9

but am not allowed to use recursion.

  • 2
    Are you allowed to use recursive functions? – J.N. May 05 '17 at 01:13
  • 1
    please show what you've tried within your post. – Ousmane D. May 05 '17 at 01:13
  • 3
    if you can't use any form of loops or `Math.pow` then the only other option is recursion? – Ousmane D. May 05 '17 at 01:14
  • 1
    Use a really long series of `if...elses`. Or a really long `switch...case` without breaks. Or how about you show what you tried to not get any more ridiculous suggestions from me. – AntonH May 05 '17 at 01:15
  • Sorry, I just added some clarification. This is kinda new to me. – Joesph Roberts May 05 '17 at 01:16
  • 6
    `^` is an XOR logical operator, not a "to the power of". – AntonH May 05 '17 at 01:17
  • 1
    Look up [Exponentiation by squaring](https://en.wikipedia.org/wiki/Exponentiation_by_squaring). – Sergey Kalinichenko May 05 '17 at 01:18
  • 1
    This will actually help you http://stackoverflow.com/questions/13182314/recursive-exponent-method if you dig up a little harder :) – jace May 05 '17 at 01:26
  • @AntonH Is this even possible? – Joesph Roberts May 05 '17 at 01:59
  • Possible duplicate of [Why is my power operator (^) not working?](http://stackoverflow.com/questions/4843304/why-is-my-power-operator-not-working) – phuclv May 05 '17 at 02:07
  • another duplicate: [Raising a number to a power in Java](http://stackoverflow.com/q/8842504/995714) – phuclv May 05 '17 at 02:07
  • So, no recursion, no loop, no function call? I don't think it is even possible to calculate power without them. – Adrian Shum May 05 '17 at 02:12
  • @JoesphRoberts with what I said, yes, but it has its limitations, and is an absolutely terrible idea. If you can't use recursion, loops or `pow`, I don't see any other way than what 'dasblinkenlight' suggested. – AntonH May 05 '17 at 03:00
  • You could also technically hard-copy all the possible combinations in a huge 2D array and just use the function as a lookup into the array, but again, terrible idea. – AntonH May 05 '17 at 03:03
  • You can compute `pow(2, n)` with `1 << n` but somehow I doubt this is what your assignment is asking for. – Radiodef May 05 '17 at 03:15
  • With those restrictions, they probably are leading you to use recursion as a means of looping. – Raphael May 05 '17 at 03:42
  • This is impossible. It's like saying Take photographs of invisible objects. –  May 05 '17 at 04:43

4 Answers4

3

Without being able to call Math.pow or using loops, the only other possibility is using recursion:

public int powerFunction(int base, int exponent) {
    if(exponent < 0){ throw new IllegalArgumentException("unsupported negative pow");  }
    if(exponent == 0){ return 1; } 
    else{
        return base * powerFunction(base, exponent - 1);
    }
}

Calling powerFunction(2, 3) will give you: 1 * 2 * 2 * 2 = 8

Raphael
  • 1,760
  • 1
  • 12
  • 21
  • 6
    Please don't solve people's homework for them. A good hint is fine, even a link to an algorithm, but providing an outright implementation is usually a disservice to them. Besides, this approach is not ideal, because passing a very high exponent has a potential of overflowing the stack. – Sergey Kalinichenko May 05 '17 at 01:22
  • The limitation with this is the assumption that the exponent is a positive integer. Still +1 – J.N. May 05 '17 at 01:23
  • Yeah, a check for positive exponents is required. `if (exponent < 0) throw something`. – Raphael May 05 '17 at 01:24
  • Base should be a double – Mad Physicist May 05 '17 at 01:29
1

You could simply use that

pow(x,y) = exp(y*log(x))

which is also part of the implementation of the power function in the math library.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
0

The recursion could help you:

public static int myPowerRec(int op1, int op2, int res) {
  if (op2 == 0) {
    return res;
  }
  else {
    return (myPowerRec(op1, op2 - 1, op1 * res));
  }
}

You will need to initialize res to 1 (myPowerRec(23, 2, 1) will give you 1 * 23 * 23). This recursion is called tail recursion and will allow you to use this function without stack problem.

Be careful, you must check op2 value before.

Ankirama
  • 498
  • 4
  • 14
0

Using a for loop:

public static int power(a, b) { // a ^ b
  int p = 1;
  for (int i = 1, i <= b; i++)
    p *= a;
  return p;
}