What would you do if you need to raise an integer to a non-integer power?
If you are dealing with non-integer powers, you should use double precision variables
and the pow() method from Java.lang.Math .
Here are
some additional methods of the BigInteger class.
See its documentation for details.
The exercises discuss some of them.
public BigInteger abs()
Returns aBigIntegerwhose values is the absolute value of thisBigInteger.
public int bitLength()
Returns the smallest number of bits it would take to represent thisBigIntegerusing two's complement, excluding the sign bit. For positive integers this is the number of bits it would take to represent the integer using the same type of representation as used inints.
public double doubleValue()
Returns the value of thisBigIntegeras adouble. The result may not be exact. It theBigIntegeris too big or too negative the value of thedoublewill be set to a special value, Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY.
public BigInteger gcd(BigInteger val)
Returns the value of the greatest common divisor of thisBigIntegerandval. The gcd is always zero or positive. It is zero when either of the arguments is zero.
public int intValue()
Returns the value of thisBigIntegeras anint. If the value is too big, the least-significant 32 bits are returned. No error indication is given if this happens, so be careful.
public long longValue()
Returns the value of thisBigIntegeras anint. If the value is too big, the least-significant 64 bits are returned. No error indication is given if this happens, so be careful.
public BigInteger mod(BigInteger modulus)
Returns aBigIntegerwhose value is (this mod m). This is different thanremainderbecause mod always returns a zero or positiveBigInteger.remainder()returns a value that is sometimes negative. The value returned bymod()andremainder()is the same if the operands are positive.
public static BigInteger valueOf(long val)
Returns aBigIntegerwhose value the same asval. This may be more convenient than using a constructor.
In addition to the above, there are bit-manipulation methods, shift right and shift left, and methods that return integers that are likely to be prime.
How can an integer only be a "likely prime" ?