I need to invert a BigInteger
.
Let's say i have BigInteger x;
and i need to calculate x.modPow(new BigInteger("-1"), p)
.
I receive the following error: java.lang.ArithmeticException: BigInteger not invertible
.
Use BigInteger.modInverse()
-- it will do what you want.
If you read the docs for BigInteger.modInverse()
(which performs the identical calculation, but more efficiently than your code; in fact presumably BigInteger.modPow()
calls modInverse()
for negative inputs before raising to a power), you'll see:
Throws: ArithmeticException - m <= 0, or this BigInteger has no multiplicative inverse mod m (that is, this BigInteger is not relatively prime to m).
If you're getting "BigInteger not invertible" this means that x and p are not relatively prime, so there is no mathematically defined inverse for the pair of numbers x and p given as input.
Possibilities:
modPow()
and modInverse()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With