Modular Inverse Calculator
Enter integers a and m to find x such that a × x ≡ 1 (mod m).
What is a modular inverse?
A modular inverse is a number that “undoes” multiplication in modular arithmetic. For integers a and m, the modular inverse of a modulo m is a value x where:
a × x ≡ 1 (mod m)
In plain terms: when you multiply a by x and divide by m, the remainder is 1.
When does an inverse exist?
A modular inverse exists only when a and m are coprime, meaning:
gcd(a, m) = 1
- If gcd is 1, an inverse exists and is unique modulo m.
- If gcd is not 1, no inverse exists.
That is why this calculator first checks the greatest common divisor before returning a result.
How this calculator works
1) Normalization
Negative values are supported. The calculator converts a into its normalized form between 0 and m-1 so that the computation is consistent.
2) Extended Euclidean Algorithm
To compute the inverse, we use the Extended Euclidean Algorithm, which finds integers x and y such that:
a·x + m·y = gcd(a, m)
When gcd is 1, the coefficient x is the modular inverse (after normalization).
3) Verification
After computing x, we verify that (a × x) mod m = 1. If this check fails, something is wrong with the input or assumptions.
Quick examples
Example A: inverse exists
Find the inverse of 3 mod 11. The result is 4 because:
3 × 4 = 12 ≡ 1 (mod 11)
Example B: inverse does not exist
Try 12 mod 8. Since gcd(12, 8) = 4, there is no integer x such that 12x leaves remainder 1 when divided by 8.
Where modular inverses are used
- Cryptography: RSA, ECC, and many key-generation routines.
- Computer science: hashing tricks, number-theory algorithms, and modular division.
- Competitive programming: factorial inverses and combinations under prime moduli.
- Mathematics: solving linear congruences and building finite fields.
Common mistakes to avoid
- Using modulus 0 or 1 (not valid for multiplicative inverses).
- Forgetting to check gcd(a, m) first.
- Assuming every nonzero number has an inverse modulo m.
- Ignoring normalization when a is negative.
Final note
This modular inverse calculator is designed for integer arithmetic and provides step-by-step Euclidean reductions so you can learn while you calculate. If you work with cryptography, modular equations, or discrete math, this is one of the most useful tools to keep handy.