How it’s calculated
The inverse of a modulo m is the number x between 0 and m − 1 with a·x ≡ 1 (mod m), the modular version of 1/a. It exists exactly when gcd(a, m) = 1. To find it, run the Euclidean algorithm on m and a, then work the steps backwards to write 1 as a combination 1 = s·m + t·a. Reducing modulo m removes the s·m term, so t·a ≡ 1 and x = t mod m.
Example: the inverse of 17 modulo 3120. 3120 = 183 × 17 + 9; 17 = 1 × 9 + 8; 9 = 1 × 8 + 1; 8 = 8 × 1 + 0, so the GCD is 1. Backwards: 1 = 9 − 8 = 9 − (17 − 9) = 2 × 9 − 17 = 2 × (3120 − 183 × 17) − 17 = 2 × 3120 − 367 × 17. So 17 × (−367) ≡ 1, and −367 + 3120 = 2753. Check: 17 × 2753 = 46,801 = 15 × 3120 + 1.
Frequently asked questions
When does a modular inverse not exist?
When a and m share a factor. For example 4 has no inverse modulo 6: 4x is always even, and an even number is never 1 more than a multiple of 6.
Where are modular inverses used?
In RSA encryption the private key d is the inverse of the public exponent e modulo φ(n); the example 17 and 3120 is the classic textbook key pair. They are also used to divide in modular arithmetic and to solve congruences such as 7x ≡ 3 (mod 48).
Is there a shortcut when m is prime?
Yes: by Fermat’s little theorem, a⁻¹ ≡ a^(m−2) (mod m) when m is prime and a is not a multiple of m. The extended Euclidean algorithm used here works for every modulus and is usually faster.
Why is the answer between 0 and m − 1?
Inverses repeat every m: if x works, so do x + m and x − m. The calculator gives the one representative from 0 to m − 1.
Embed this calculator
Add this free calculator to your own website. Copy the code below into your page’s HTML:
Need this calculator branded, changed or built around your own prices?See custom business solutions →
Sources
- Elementary Number Theory (Raji), §3.3 Linear Congruences (the modular inverse; 7 is its own inverse modulo 48) — LibreTexts Mathematics
- Elementary Number Theory (Raji), §1.6 The Euclidean Algorithm (writing the GCD as a linear combination) — LibreTexts Mathematics
Formulas are taken from the free public references above. Results are provided “as is” for informational and educational purposes only. See our disclaimer.
Added · Last updated · How we check our calculators
Spotted a mistake or missing option? Report a problem· Suggest a calculator