01

What a modular inverse is

Working modulo m means keeping only remainders after division by m. In that world, numbers wrap around: after m − 1 comes 0 again. Multiplication still works, but division needs a new definition, because there is no fraction like 1/3 in a set of whole numbers.

Definitiona · x ≡ 1 (mod m)
x is written a⁻¹ and is unique modulo m

So a⁻¹ is the number that, multiplied by a, leaves a remainder of 1. Modulo 11, the multiples of 3 are 3, 6, 9, 1 — and the fourth one lands on 1, which means 3 × 4 = 12 ≡ 1. Therefore 3⁻¹ ≡ 4 (mod 11).

A modular inverse exists only when a and m share no common factor, that is, when gcd(a, m) = 1. If they share a factor, no multiple of a can ever be one more than a multiple of m, and the tool says so explicitly rather than returning a wrong number.

02

The extended Euclidean algorithm

The standard way to find a modular inverse is to run the Euclidean algorithm and then back-substitute. The Euclidean part computes gcd(a, m) through repeated division; the extended part also tracks the coefficients that express that gcd as a combination of a and m.

  1. Divide m by a and record the quotient and remainder.
  2. Repeat with the previous divisor and remainder until the remainder is zero.
  3. The last non-zero remainder is gcd(a, m). If it is not 1, stop — there is no inverse.
  4. Back-substitute to write 1 = a·x + m·y. The coefficient x is the inverse, reduced modulo m.

For a = 3 and m = 11: 11 = 3 × 3 + 2, then 3 = 1 × 2 + 1, then 2 = 2 × 1 + 0. Back-substituting gives 1 = 3 − 1×2 = 3 − (11 − 3×3) = 4×3 − 1×11. So 4 × 3 ≡ 1, and 3⁻¹ ≡ 4.

Inverses modulo 11
aa⁻¹ mod 11Check a·a⁻¹
111
2612 ≡ 1
3412 ≡ 1
4312 ≡ 1
5945 ≡ 1
6212 ≡ 1
7856 ≡ 1
8756 ≡ 1
9545 ≡ 1
1010100 ≡ 1

Every non-zero residue has an inverse here because 11 is prime. Notice the symmetry: 2 and 6 are partners, 3 and 4 are partners, and 10 is its own inverse since 10 ≡ −1 and (−1)² = 1.

03

Solving a·x ≡ b (mod m)

Once the inverse is known, any linear congruence follows: multiply both sides by a⁻¹ to get x ≡ b·a⁻¹ (mod m). For 3x ≡ 7 (mod 11), multiply by 4: x ≡ 28 ≡ 6 (mod 11). Check: 3 × 6 = 18 = 11 + 7. Correct.

When gcd(a, m) = g > 1 the situation changes. A solution exists only if g divides b, and then there are exactly g distinct solutions modulo m, spaced m/g apart. The tool reports the reduced modulus so you can see the family.

One inverse, many congruences. The inverse depends only on a and m. Change b and you change the answer by a single multiplication — which is why computing the inverse once and reusing it is the efficient approach.

04

The Fermat shortcut for prime moduli

When m is prime and a is not a multiple of m, Fermat's little theorem gives a^(m−1) ≡ 1 (mod m). Dividing by a, that becomes a^(m−2) ≡ a⁻¹ (mod m). So modulo 11, 3⁻¹ = 3⁹ mod 11, which is 4.

This is not faster by hand, but it is much faster on a computer once modular exponentiation is available, because exponentiation by squaring needs only O(log m) multiplications. The extended Euclidean algorithm is also O(log m), so in practice the choice comes down to which primitive your library already has.

05

Why modular inverses matter

  • RSA encryption — the private exponent d is the modular inverse of e modulo φ(n). That single step is the whole key-generation procedure.
  • Error-correcting codes — Reed–Solomon decoding solves linear systems over finite fields, which requires inverses of field elements.
  • Hashing — universal hash families use multiplication by a random invertible element modulo a prime.
  • Cryptographic protocols — Diffie–Hellman, elliptic curves and digital signatures all divide in a finite field, and division is multiplication by an inverse.
  • Competitive programming — combinatorics modulo 10⁹+7 relies on factorials and their inverses.

In every case the arithmetic is exact. There is no floating point involved, so results are correct to the last digit — which is precisely why cryptography is built on modular arithmetic rather than real numbers.

06

Inverses modulo a composite

Primality is not required for a modular inverse; coprimality is. Modulo 10, the numbers 1, 3, 7 and 9 have inverses, while 2, 4, 5, 6 and 8 do not — because each of those shares a factor with 10.

Inverses modulo 10
agcd(a, 10)a⁻¹ mod 10Check
1111 × 1 = 1
3173 × 7 = 21 ≡ 1
7137 × 3 = 21 ≡ 1
9199 × 9 = 81 ≡ 1
22noneEvery multiple is even
55noneMultiples end in 0 or 5

The pattern is worth noticing: the invertible residues are exactly those coprime with the modulus, and their count is Euler's totient φ(m). For a prime p that count is p − 1, which is why every non-zero residue has an inverse modulo a prime.

07

The inverse in RSA key generation

RSA needs one modular inverse and nothing else. Choose two primes p and q, compute n = pq and φ(n) = (p−1)(q−1), pick a public exponent e coprime with φ(n), then the private exponent is d = e⁻¹ mod φ(n).

A small worked key pair
StepValue
Choose p, q61, 53
n = pq3233
φ(n) = (p−1)(q−1)3120
Public e17
Private d = 17⁻¹ mod 31202753
Check 17 × 275346 801 ≡ 1 (mod 3120)

Encryption is c = m^e mod n and decryption is m = c^d mod n. The two exponents cancel because ed ≡ 1 (mod φ(n)), which is exactly the definition of a modular inverse. Real keys use 2048-bit primes, but the arithmetic is identical to the table above.

Everything rests on the fact that multiplying two large primes is easy while factoring their product is not. The inverse itself is computed in microseconds by the extended Euclidean algorithm; it is the factorisation that is hard.

08

A second worked example, by hand

Find 17⁻¹ mod 3120. This is the RSA key-generation step, and walking it manually shows how little work the algorithm actually does.

Euclidean divisions
StepDivisionQuotientRemainder
13120 = 183 × 17 + 91839
217 = 1 × 9 + 818
39 = 1 × 8 + 111
48 = 8 × 1 + 080

The last non-zero remainder is 1, so the numbers are coprime and an inverse exists. Back-substituting: 1 = 9 − 8 = 9 − (17 − 9) = 2·9 − 17 = 2·(3120 − 183·17) − 17 = 2·3120 − 367·17.

So −367·17 ≡ 1 (mod 3120), which means 17⁻¹ ≡ −367 ≡ 3120 − 367 = 2753. That is the private exponent from the RSA example, derived in four divisions and three substitutions.

Four steps, any size. The number of divisions grows with the logarithm of the modulus, so a 2048-bit key needs roughly 2000 division steps rather than an impossible search. That efficiency is what makes public-key cryptography practical.

09

Verifying by multiplication

The check is one multiplication and one remainder: a × a⁻¹ mod m must equal 1. For 3⁻¹ mod 11 that is 3 × 4 = 12, and 12 − 11 = 1. If the product reduces to anything else, the inverse is wrong.

That check is worth doing by hand even when a tool produced the answer, because it takes seconds and catches transcription errors that a second tool run would reproduce. The tool prints the line for you; reading it is the point.

?

Frequently asked questions

What is the inverse of 3 modulo 11?

It is 4, because 3 × 4 = 12, and 12 leaves a remainder of 1 when divided by 11. No other number between 0 and 10 has that property.

Why does 6 have no inverse modulo 9?

Because gcd(6, 9) = 3, not 1. Every multiple of 6 modulo 9 is a multiple of 3, so it can never equal 1. Coprimality is both necessary and sufficient for an inverse to exist.

Can the inverse be negative?

The extended Euclidean algorithm often produces a negative coefficient. Adding m brings it into the standard range 0 to m − 1, and both describe the same residue class.

How many solutions does a·x ≡ b (mod m) have?

If gcd(a, m) divides b, there are exactly gcd(a, m) solutions modulo m. If it does not divide b, there are none.

Does this work when the modulus is not prime?

Yes, as long as a is coprime with m. Primality is not required — coprimality is. For example 3⁻¹ mod 10 is 7, because 21 ≡ 1.

What is the inverse of 1?

Always 1, for every modulus. And the inverse of m − 1 is always m − 1, because (m − 1)² = m² − 2m + 1 ≡ 1.