I'm using a BigInteger object. With normal ints or longs, I can use Math.pow(number, 1/nth root) to get the nth root. However, this will not work with a BigInteger. Is there a way I can do this?
I do not actually need the root, just to know if it is a perfect power. I'm using this to figure out if the given BigInteger is a perfect square/cube/etc.
Newton's method works perfectly well with integers; here we compute the largest number s for which sk does not exceed n, assuming both k and n are positive:
For instance,
iroot(4, 624)
returns 4 andiroot(4, 625)
returns 5. Then you can perform the exponentiation and check the result:For instance,
perfectPower(2, 625)
andperfectPower(4, 625)
are both true, butperfectPower(3, 625)
is false.I'll leave it to you to translate to Java BigInteger.