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
Parameter | Optional/Required | Description |
---|---|---|
modulus | required | the 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,
- We create a BigInt object,
num
, initialized with 7. - We also create a BigInt object,
modulus
, initialized with 11. - We then use the
modInverse
method to calculate the modular multiplicative inverse ofnum
modulomodulus
. - 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,
- We create a BigInt object,
num
, initialized with 25. - We also create a BigInt object,
modulus
, initialized with 8. - We then use the
modInverse
method to calculate the modular multiplicative inverse ofnum
modulomodulus
. - 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,
- We create a BigInt object,
num
, initialized with 11. - We also create a BigInt object,
modulus
, initialized with 13. - We then use the
modInverse
method to calculate the modular multiplicative inverse ofnum
modulomodulus
. - 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.