Prerequisite : BigInteger Basics
The modPow() 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).
public BigInteger modInverse(BigInteger m)
Parameters: m – the modulus.
Return Value: This method returns a BigInteger object whose value is ((this)^(-1) mod m).
- ArithmeticException – m <= 0, or this BigInteger has no multiplicative inverse mod m (that is, this BigInteger is not relatively prime to m).
Below programs illustrate the BigInteger.modInverse() method:
Program 1 :
8 ^ -1 % 21 = 8
Program 2 :
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)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.string.replace() method in Java
- Java.util.ArrayList.add() Method in Java
- Java.util.ArrayList.addall() method in Java
- Java.util.BitSet.set() method in Java
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.math.BigInteger.probablePrime() method in Java
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Method Class | getName() Method in Java
- Method Class | toGenericString() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai