Congruence
Two integers a and b are congruent modulo n if n divides their difference:
a≡b(modn)⟺n∣(a−b)
Equivalently: a and b have the same remainder when divided by n.
Examples
- 17≡5(mod12) (both leave remainder 5)
- −3≡9(mod12) (difference is 12)
- 100≡0(mod10)
Properties of Congruence
Equivalence Relation
- Reflexive: a≡a(modn)
- Symmetric: If a≡b, then b≡a
- Transitive: If a≡b and b≡c, then a≡c
Arithmetic Properties
If a≡b(modn) and c≡d(modn), then:
a+c≡b+d(modn)
a−c≡b−d(modn)
ac≡bd(modn)
ak≡bk(modn) for any k≥0
Division (Be Careful!)
ac≡bc(modn) does NOT imply a≡b(modn)!
Correct rule: ac≡bc(modn) implies a≡b(modn/d) where d=gcd(c,n).
If gcd(c,n)=1, then we can divide: a≡b(modn).
Residue Classes
The residue class of a modulo n:
[a]n={b∈Z:b≡a(modn)}
The set of residue classes: Zn={[0],[1],…,[n−1]}
We often write just 0,1,…,n−1 for convenience.
Modular Arithmetic Operations
Addition Table (mod 4)
| + | 0 | 1 | 2 | 3 |
|---|
| 0 | 0 | 1 | 2 | 3 |
| 1 | 1 | 2 | 3 | 0 |
| 2 | 2 | 3 | 0 | 1 |
| 3 | 3 | 0 | 1 | 2 |
Multiplication Table (mod 5)
| × | 0 | 1 | 2 | 3 | 4 |
|---|
| 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 1 | 2 | 3 | 4 |
| 2 | 0 | 2 | 4 | 1 | 3 |
| 3 | 0 | 3 | 1 | 4 | 2 |
| 4 | 0 | 4 | 3 | 2 | 1 |
Modular Inverse
The modular inverse of a modulo n is an integer a−1 such that:
a⋅a−1≡1(modn)
Existence
a−1 exists if and only if gcd(a,n)=1.
Finding the Inverse
Use the extended Euclidean algorithm to find x such that:
ax+ny=1
Then x is the inverse of a modulo n.
Example
Find 7−1(mod11):
7⋅8=56=5⋅11+1≡1(mod11)
So 7−1≡8(mod11).
Linear Congruences
Solve: ax≡b(modn)
Solution
Let d=gcd(a,n).
No solution if d∤b.
If d∣b: There are exactly d solutions modulo n.
Reduce to: dax≡db(moddn)
Use modular inverse to solve.
Example
Solve 6x≡4(mod10)
gcd(6,10)=2 and 2∣4, so solutions exist.
Reduce: 3x≡2(mod5)
3−1≡2(mod5) (since 3⋅2=6≡1)
x≡2⋅2≡4(mod5)
Solutions mod 10: x≡4 or x≡9.
Chinese Remainder Theorem
If n1,n2,…,nk are pairwise coprime, then the system:
x≡a1(modn1)
x≡a2(modn2)
⋮
x≡ak(modnk)
has a unique solution modulo N=n1n2⋯nk.
Construction
x=∑i=1kaiNiyi(modN)
where Ni=N/ni and yi=Ni−1(modni).
Example
Solve: x≡2(mod3), x≡3(mod5), x≡2(mod7)
N=3⋅5⋅7=105
N1=35, y1=35−1≡2−1≡2(mod3)
N2=21, y2=21−1≡1−1≡1(mod5)
N3=15, y3=15−1≡1−1≡1(mod7)
x=2⋅35⋅2+3⋅21⋅1+2⋅15⋅1=140+63+30=233≡23(mod105)
Fast Modular Exponentiation
Compute ak(modn) efficiently using binary representation of k.
Algorithm (Square and Multiply)
power(a, k, n):
result = 1
a = a mod n
while k > 0:
if k is odd:
result = (result * a) mod n
k = k / 2 (integer division)
a = (a * a) mod n
return result
Time: O(logk) multiplications.
Example
313(mod7)
13=11012
31=3
32=9≡2
34≡22=4
38≡42=16≡2
313=38⋅34⋅31≡2⋅4⋅3=24≡3(mod7)