Fermat's Little Theorem
If p is prime and a is not divisible by p, then:
ap−1≡1(modp)
Equivalently, for any integer a:
ap≡a(modp)
Examples
- 26≡1(mod7) (since 7 is prime, gcd(2,7)=1)
- 310≡1(mod11)
- 54≡1(mod5)? No! gcd(5,5)=1
Proof Sketch
Consider the set {a,2a,3a,…,(p−1)a}(modp).
These are all distinct and non-zero (since gcd(a,p)=1).
So they're a permutation of {1,2,…,p−1}.
a⋅2a⋅3a⋯(p−1)a≡1⋅2⋅3⋯(p−1)(modp)
ap−1(p−1)!≡(p−1)!(modp)
ap−1≡1(modp)
(We can divide by (p−1)! since gcd((p−1)!,p)=1.)
Applications
Computing Modular Inverses
If p is prime and gcd(a,p)=1:
a−1≡ap−2(modp)
Reducing Large Exponents
ak(modp) where k is huge:
ak≡akmod(p−1)(modp)
Example
Compute 3100(mod7):
100=6×16+4
3100=(36)16⋅34≡116⋅81≡81≡4(mod7)
Euler's Theorem (Generalization)
For any positive integer n and integer a with gcd(a,n)=1:
aϕ(n)≡1(modn)
where ϕ(n) is Euler's totient function.
Euler's Totient Function
ϕ(n) = count of integers from 1 to n coprime to n.
Computing ϕ(n)
For prime p:
ϕ(p)=p−1
For prime power pk:
ϕ(pk)=pk−pk−1=pk−1(p−1)
Multiplicative property: If gcd(m,n)=1:
ϕ(mn)=ϕ(m)ϕ(n)
General formula: For n=p1a1p2a2⋯pkak:
ϕ(n)=n∏p∣n(1−p1)=n(1−p11)(1−p21)⋯(1−pk1)
Examples
ϕ(10)=10⋅(1−21)(1−51)=10⋅21⋅54=4
The numbers coprime to 10: 1, 3, 7, 9.
ϕ(12)=12⋅(1−21)(1−31)=12⋅21⋅32=4
Properties of ϕ(n)
∑d∣nϕ(d)=n
The divisors of n partition {1,2,…,n} by gcd with n.
Fermat as Special Case of Euler
When n=p (prime): ϕ(p)=p−1
So Euler's theorem becomes Fermat's theorem.
Carmichael Numbers
A Carmichael number is a composite n such that:
an−1≡1(modn)
for all a coprime to n.
Examples
561, 1105, 1729, ...
These "fool" the Fermat primality test.
Detection
Carmichael numbers are square-free with at least 3 prime factors.
Order of an Element
The order of a modulo n is the smallest positive k such that:
ak≡1(modn)
Written ordn(a).
Properties
- ordn(a) divides ϕ(n)
- am≡1(modn) iff ordn(a)∣m
Primitive Roots
g is a primitive root mod n if ordn(g)=ϕ(n).
Primitive roots exist for: n=1,2,4,pk,2pk (where p is odd prime).