![]() |
VOOZH | about |
Prerequisite : BigInteger Basics The modInverse() method returns modular multiplicative inverse of this, mod m. This method throws an ArithmeticException if m <= 0 or this has no multiplicative inverse mod m (i.e., gcd(this, m) != 1).
Syntax:
public BigInteger modInverse(BigInteger m)
Parameters: m - the modulus.
Return Value: This method returns a BigInteger object whose value is ((this)^(-1) mod m).
Exception:
Below programs illustrate the BigInteger.modInverse() method:
Program 1 :
Output:
8 ^ -1 % 21 = 8
Program 2 :
Output :
Exception in thread "main" java.lang.ArithmeticException: BigInteger not invertible. at java.math.MutableBigInteger.modInverse(Unknown Source) at java.math.MutableBigInteger.mutableModInverse(Unknown Source) at java.math.BigInteger.modInverse(Unknown Source) at BigInteger.GFG2.main(GFG2.java:23)
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#modInverse(java.math.BigInteger)