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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
UseBigInteger.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 presumablyBigInteger.modPow()callsmodInverse()for negative inputs before raising to a power), you’ll see: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()andmodInverse()