TExcelTAD
Home › Discrete Mathematics › Inverses, Fermat and Euler

Inverses, Fermat and Euler

Division modulo nn means multiplying by an inverse. This lesson finds inverses with the extended Euclidean algorithm, then proves two theorems about powers that make modular exponentiation predictable: Fermat's little theorem for primes and Euler's theorem for any modulus. Both are needed for RSA in the next lesson.

Modular inverses

An inverse of kk modulo nn is a number k−1k^{-1} with k⋅k−1≡1(modn)k \cdot k^{-1} \equiv 1 \pmod{n}.

Theorem. kk has an inverse modulo nn if and only if gcd⁡(k,n)=1\gcd(k, n) = 1.

Proof. If gcd⁡(k,n)=1\gcd(k, n) = 1, Bézout gives sk+tn=1sk + tn = 1, so sk≡1(modn)sk \equiv 1 \pmod{n}: ss is an inverse. Conversely, if sk≡1sk \equiv 1, then sk−1=tnsk - 1 = tn, so any common divisor of kk and nn divides 1. ■\blacksquare

The proof is an algorithm: run the extended Euclidean algorithm and read off ss.

python · try it yourself
def inverse(k, n):
    r0, r1, s0, s1 = n, k % n, 0, 1
    while r1:
        q = r0 // r1
        r0, r1 = r1, r0 - q * r1
        s0, s1 = s1, s0 - q * s1
    if r0 != 1:
        raise ValueError(f"{k} has no inverse mod {n} (gcd = {r0})")
    return s0 % n

print("7^-1 mod 26 =", inverse(7, 26), " check:", 7 * inverse(7, 26) % 26)
print("17^-1 mod 3120 =", inverse(17, 3120), " built-in:", pow(17, -1, 3120))
try:
    inverse(6, 15)
except ValueError as e:
    print(e)
7^-1 mod 26 = 15  check: 1
17^-1 mod 3120 = 2753  built-in: 2753
6 has no inverse mod 15 (gcd = 3)

With inverses you can solve linear congruences: 7x≡3(mod26)7x \equiv 3 \pmod{26} gives x≡3⋅7−1=3⋅15=45≡19(mod26)x \equiv 3 \cdot 7^{-1} = 3 \cdot 15 = 45 \equiv 19 \pmod{26}.

Fermat's little theorem

Theorem. If pp is prime and p∤ap \nmid a, then

ap−1≡1(modp)a^{p-1} \equiv 1 \pmod{p}

Proof. Multiplying by aa permutes the nonzero residues 1,2,…,p−11, 2, \ldots, p - 1 (it is injective because aa has an inverse, and an injective map of a finite set to itself is a bijection). So the products are equal:

(1⋅a)(2⋅a)⋯((p−1)⋅a)≡1⋅2⋯(p−1)(modp)(1 \cdot a)(2 \cdot a) \cdots ((p-1) \cdot a) \equiv 1 \cdot 2 \cdots (p - 1) \pmod{p}

The left side is (p−1)! ap−1(p-1)! \, a^{p-1}. Since (p−1)!(p-1)! is coprime to pp, cancel it to get ap−1≡1a^{p-1} \equiv 1. ■\blacksquare

Consequences: a−1≡ap−2(modp)a^{-1} \equiv a^{p-2} \pmod p (another way to compute inverses), and exponents can be reduced modulo p−1p - 1: ae≡ae mod (p−1)a^{e} \equiv a^{e \bmod (p-1)}.

python · try it yourself
p = 13
for a in [2, 5, 12]:
    print(f"a={a}: a^(p-1) mod p = {pow(a, p - 1, p)}, inverse via a^(p-2): {pow(a, p - 2, p)}")
print("2^1000 mod 13 =", pow(2, 1000, 13), " = 2^(1000 mod 12) mod 13 =", pow(2, 1000 % 12, 13))
# the converse fails: 561 = 3*11*17 passes the Fermat test for every coprime base
print("561 passes for base 2?", pow(2, 560, 561) == 1)
a=2: a^(p-1) mod p = 1, inverse via a^(p-2): 7
a=5: a^(p-1) mod p = 1, inverse via a^(p-2): 8
a=12: a^(p-1) mod p = 1, inverse via a^(p-2): 12
2^1000 mod 13 = 3  = 2^(1000 mod 12) mod 13 = 3
561 passes for base 2? True

The last line shows a Carmichael number: composite, yet an−1≡1(modn)a^{n-1} \equiv 1 \pmod n for every aa coprime to it. That is why real primality tests use the stronger Miller-Rabin test.

Euler's theorem

For general nn, let Euler's totient ϕ(n)\phi(n) be the number of integers in {1,…,n}\{1, \ldots, n\} coprime to nn. Examples: ϕ(p)=p−1\phi(p) = p - 1 for a prime, ϕ(pk)=pk−pk−1\phi(p^k) = p^k - p^{k-1}, and ϕ\phi is multiplicative for coprime arguments, so

ϕ(pq)=(p−1)(q−1)for distinct primes p,q\phi(pq) = (p - 1)(q - 1) \quad \text{for distinct primes } p, q

Theorem (Euler). If gcd⁡(a,n)=1\gcd(a, n) = 1, then aϕ(n)≡1(modn)a^{\phi(n)} \equiv 1 \pmod{n}.

The proof is Fermat's argument applied to the ϕ(n)\phi(n) residues coprime to nn, which multiplication by aa permutes.

python · try it yourself
from math import gcd
from sympy import totient

def phi(n):
    return sum(1 for k in range(1, n + 1) if gcd(k, n) == 1)

for n in [10, 12, 13, 15, 3233]:
    a = 7
    print(f"phi({n}) = {phi(n)} (sympy {totient(n)}),  7^phi(n) mod n = {pow(a, phi(n), n)}")
phi(10) = 4 (sympy 4),  7^phi(n) mod n = 1
phi(12) = 4 (sympy 4),  7^phi(n) mod n = 1
phi(13) = 12 (sympy 12),  7^phi(n) mod n = 1
phi(15) = 8 (sympy 8),  7^phi(n) mod n = 1
phi(3233) = 3120 (sympy 3120),  7^phi(n) mod n = 1

3233=61⋅533233 = 61 \cdot 53, and ϕ(3233)=60⋅52=3120\phi(3233) = 60 \cdot 52 = 3120.

In software

Cryptographic libraries compute inverses with the extended Euclidean algorithm (or via Fermat when the modulus is prime, as in elliptic-curve code over a prime field). Python offers both: pow(a, -1, n) and pow(a, p - 2, p). Fermat's theorem is also behind a cheap first filter in primality testing.

Exercises

Exercise 1: An inverse by hand

Find the inverse of 5 modulo 17.

Answer
Solution

5⋅7=35=2⋅17+15 \cdot 7 = 35 = 2 \cdot 17 + 1. Via Fermat: 515 mod 175^{15} \bmod 17 gives the same 7.

Exercise 2: Solve a congruence

Solve 4x≡7(mod9)4x \equiv 7 \pmod{9}.

Answer

x≡4(mod9)x \equiv 4 \pmod 9.

Solution

gcd⁡(4,9)=1\gcd(4, 9) = 1; 4⋅7=28≡14 \cdot 7 = 28 \equiv 1, so 4−1≡74^{-1} \equiv 7. Then x≡7⋅7=49≡4(mod9)x \equiv 7 \cdot 7 = 49 \equiv 4 \pmod 9. Check: 4⋅4=16≡74 \cdot 4 = 16 \equiv 7.

Exercise 3: Big exponent

Compute 31000 mod 73^{1000} \bmod 7 by hand with Fermat.

Answer
Solution

By Fermat, 36≡1(mod7)3^6 \equiv 1 \pmod 7. 1000=6⋅166+41000 = 6 \cdot 166 + 4, so 31000≡34=81≡4(mod7)3^{1000} \equiv 3^4 = 81 \equiv 4 \pmod 7 (81=11⋅7+481 = 11 \cdot 7 + 4).

Exercise 4: Totients (code)

Compute ϕ(n)\phi(n) for n=1,…,20n = 1, \ldots, 20 and check that ∑d∣nϕ(d)=n\sum_{d \mid n} \phi(d) = n for each.

Answer

The identity holds for all of them.

Solution

Each k∈{1,…,n}k \in \{1, \ldots, n\} has gcd⁡(k,n)=d\gcd(k, n) = d for exactly one divisor dd, and the kk with gcd⁡(k,n)=d\gcd(k, n) = d correspond to the numbers coprime to n/dn/d, so the classes have sizes ϕ(n/d)\phi(n/d).

python · try it yourself
from math import gcd

phi = lambda n: sum(1 for k in range(1, n + 1) if gcd(k, n) == 1)
print([phi(n) for n in range(1, 21)])
print(all(sum(phi(d) for d in range(1, n + 1) if n % d == 0) == n for n in range(1, 21)))
[1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6, 18, 8]
True

Test yourself

When does kk have an inverse modulo nn?

  1. Always
  2. When k<nk < n
  3. When gcd⁡(k,n)=1\gcd(k, n) = 1
  4. When nn is even

By Fermat, a16 mod 17a^{16} \bmod 17 for aa not divisible by 17 is…

  1. 0
  2. 1
  3. aa
  4. 16

What is ϕ(35)\phi(35)?

  1. 34
  2. 24
  3. 12
  4. 35