1

I have been staring at this code and cannot figure out what is wrong with it, maybe a fresh pair of eyes could help.

public static BigInteger powerOfBigInteger (BigInteger base, BigInteger power){
    if (power == BigInteger.valueOf(0)){
        return BigInteger.valueOf(1);
    }

    if (power == BigInteger.valueOf(1)){
        return base;
    }

    BigInteger x = BigInteger.valueOf(1);
    while (x != power ){
        base.multiply(base);
        x.add(BigInteger.valueOf(1));
        System.out.println(x + " " + power);
                   return base;
    } 

    return base;

I run this and apparently x never equals power. Any help is appreciated.

user1446565
  • 53
  • 1
  • 3
  • 1
    Why implement this method? `BigInteger` already has a `pow(int)` method, and for any power too big to fit in an `int` will run you out of memory anyway. – Louis Wasserman Jun 14 '12 at 01:56

4 Answers4

5

You should do

if (BigInteger.ZERO.equals(power)) return BigInteger.ONE;
if (BigInteger.ONE.equals(power))  return base;

and you have to accumulate the result of your multiplication and addition because BigInteger is immutable and returns new BigInteger instance

The infinite loop is because your x NEVER changes - should be

x = x.add(BigInteger.ONE);

multiplication should change to

result = result.mulitply(base);

where initial result value should be BigInteger.ONE

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60
1

One thing I see is you are comparing using == instead of .equals. Compare objects with equals.

public static BigInteger powerOfBigInteger (BigInteger base, BigInteger power){
    if (power.equals(BigInteger.valueOf(0))){
        return BigInteger.valueOf(1);
    }

    if (power.equals(BigInteger.valueOf(1))){
        return base;
    }

    BigInteger x = BigInteger.valueOf(1);
    while (!x.equals(power)){
        base.multiply(base);
        x.add(BigInteger.valueOf(1));
        System.out.println(x + " " + power);
                   return base;
    } 

    return base;
}
tjg184
  • 4,508
  • 1
  • 27
  • 54
1

I see you're using == with BigInteger. Don't do this. Use .equals(other) instead. BigInteger is not a primitive that can be compared with normal operators (you're just comparing the object references, which are most likely not going to be equal). Also, you're not setting anything when you perform the calculations on your BigInteger instances; they don't operate on the local object and store the result in the local object. You need to store the returned object.

Problematic lines:

  • base.multiply(base);
  • x.add(BigInteger.valueOf(1));

There's also some discussion in this other post on SO re. BigInteger to the power of BigInteger: BigInteger.pow(BigInteger)?

Community
  • 1
  • 1
Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
0

Maybe I'm missing something but isn't it as simple as the fact that he's doing base.multiply(base). When figuring a power, the base should stay as given. Example 2^3 should generate a sequence of 2,4,8 but this code would generate 2,4,16 because after the first loop base is four, and four.multiple(four) is 16, not 8. I must be missing something.

Thomas Jay Rush
  • 401
  • 4
  • 12