Dart BigInt modInverse()
Syntax & Examples

BigInt.modInverse() method

The `modInverse` method calculates the modular multiplicative inverse of a big integer modulo a given modulus.


Syntax of BigInt.modInverse()

The syntax of BigInt.modInverse() method is:

 BigInt modInverse(BigInt modulus) 

This modInverse() method of BigInt returns the modular multiplicative inverse of this big integer modulo modulus.

Parameters

ParameterOptional/RequiredDescription
modulusrequiredthe modulus to calculate the modular multiplicative inverse with

Return Type

BigInt.modInverse() returns value of type BigInt.



✐ Examples

1 Calculate Modular Multiplicative Inverse

In this example,

  1. We create a BigInt object, num, initialized with 7.
  2. We also create a BigInt object, modulus, initialized with 11.
  3. We then use the modInverse method to calculate the modular multiplicative inverse of num modulo modulus.
  4. We print the result to standard output.

Dart Program

void main() {
  BigInt num = BigInt.from(7);
  BigInt modulus = BigInt.from(11);
  BigInt inverse = num.modInverse(modulus);
  print('Modular inverse of 7 modulo 11: $inverse');
}

Output

Modular inverse of 7 modulo 11: 8

2 Calculate Modular Multiplicative Inverse with Negative Numbers

In this example,

  1. We create a BigInt object, num, initialized with 25.
  2. We also create a BigInt object, modulus, initialized with 8.
  3. We then use the modInverse method to calculate the modular multiplicative inverse of num modulo modulus.
  4. We print the result to standard output.

Dart Program

void main() {
  BigInt num = BigInt.from(25);
  BigInt modulus = BigInt.from(8);
  BigInt inverse = num.modInverse(modulus);
  print('Modular inverse of 25 modulo 8: $inverse');
}

Output

Modular inverse of 25 modulo 8: 1

3 Calculate Modular Multiplicative Inverse of Larger Numbers

In this example,

  1. We create a BigInt object, num, initialized with 11.
  2. We also create a BigInt object, modulus, initialized with 13.
  3. We then use the modInverse method to calculate the modular multiplicative inverse of num modulo modulus.
  4. We print the result to standard output.

Dart Program

void main() {
  BigInt num = BigInt.from(11);
  BigInt modulus = BigInt.from(13);
  BigInt inverse = num.modInverse(modulus);
  print('Modular inverse of 11 modulo 13: $inverse');
}

Output

Modular inverse of 11 modulo 13: 6

Summary

In this Dart tutorial, we learned about modInverse() method of BigInt: the syntax and few working examples with output and detailed explanation for each example.