BigInteger modInverse() in Java

modInverse(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.modInverse(BigInteger m)

This method takes one argument of type BigInteger as its parameter. This method returns a BigInteger whose value is (this^-1 mod m).

Parameters: One parameter is required for this method.

m: the modulus.

Returns: this^-1 mod m.

Throws: ArithmeticException - m ≤ 0, or this BigIntegerhas no multiplicative inverse mod m (that is, this BigInteger is not relatively prime to m).

For Example:

BigInteger bigInteger = new BigInteger("1234")

BigInteger m = new BigInteger("7")

bigInteger.modInverse(m) = > It returns 4. 

Approach

Java

import java.math.BigInteger;

public class BigIntegermodInverse {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");

        BigInteger m = new BigInteger("7");
        System.out.println(bigInteger.modInverse(m));
    }
}

Output:

4

No comments:

Post a Comment