Dart int modInverse()
Syntax & Examples
int.modInverse() method
The `modInverse` method in Dart returns the modular multiplicative inverse of this integer modulo the given modulus.
Syntax of int.modInverse()
The syntax of int.modInverse() method is:
int modInverse(int modulus)
This modInverse() method of int returns the modular multiplicative inverse of this integer modulo modulus.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
modulus | required | The modulus value to compute the inverse. |
Return Type
int.modInverse() returns value of type int
.
✐ Examples
1 Compute modular multiplicative inverse
In this example,
- We create an integer variable
num
with the value 3 and a modulus variable with the value 11. - We use the
modInverse()
method with the modulus as the argument to compute the modular multiplicative inverse. - We then print the result to standard output.
Dart Program
void main() {
int num = 3;
int modulus = 11;
int inverse = num.modInverse(modulus);
print('Modular multiplicative inverse of $num modulo $modulus: $inverse');
}
Output
Modular multiplicative inverse of 3 modulo 11: 4
2 Compute modular multiplicative inverse
In this example,
- We create an integer variable
num
with the value 5 and a modulus variable with the value 12. - We use the
modInverse()
method with the modulus as the argument to compute the modular multiplicative inverse. - We then print the result to standard output.
Dart Program
void main() {
int num = 5;
int modulus = 12;
int inverse = num.modInverse(modulus);
print('Modular multiplicative inverse of $num modulo $modulus: $inverse');
}
Output
Modular multiplicative inverse of 5 modulo 12: 5
3 Compute modular multiplicative inverse
In this example,
- We create an integer variable
num
with the value 7 and a modulus variable with the value 10. - We use the
modInverse()
method with the modulus as the argument to compute the modular multiplicative inverse. - We then print the result to standard output.
Dart Program
void main() {
int num = 7;
int modulus = 10;
int inverse = num.modInverse(modulus);
print('Modular multiplicative inverse of $num modulo $modulus: $inverse');
}
Output
Modular multiplicative inverse of 7 modulo 10: 3
Summary
In this Dart tutorial, we learned about modInverse() method of int: the syntax and few working examples with output and detailed explanation for each example.