Number TheoryTopic #32 of 40

Modular Arithmetic

Congruence, modular operations, linear congruences, and the Chinese Remainder Theorem.

Congruence

Two integers aa and bb are congruent modulo nn if nn divides their difference: a≡b(modn)  ⟺  n∣(a−b)a \equiv b \pmod{n} \iff n \mid (a - b)

Equivalently: aa and bb have the same remainder when divided by nn.

Examples

  • 17≡5(mod12)17 \equiv 5 \pmod{12} (both leave remainder 5)
  • −3≡9(mod12)-3 \equiv 9 \pmod{12} (difference is 12)
  • 100≡0(mod10)100 \equiv 0 \pmod{10}

Properties of Congruence

Equivalence Relation

  • Reflexive: a≡a(modn)a \equiv a \pmod{n}
  • Symmetric: If a≡ba \equiv b, then b≡ab \equiv a
  • Transitive: If a≡ba \equiv b and b≡cb \equiv c, then a≡ca \equiv c

Arithmetic Properties

If a≡b(modn)a \equiv b \pmod{n} and c≡d(modn)c \equiv d \pmod{n}, then:

a+c≡b+d(modn)a + c \equiv b + d \pmod{n} a−c≡b−d(modn)a - c \equiv b - d \pmod{n} ac≡bd(modn)ac \equiv bd \pmod{n} ak≡bk(modn) for any k≥0a^k \equiv b^k \pmod{n} \text{ for any } k \geq 0

Division (Be Careful!)

ac≡bc(modn)ac \equiv bc \pmod{n} does NOT imply a≡b(modn)a \equiv b \pmod{n}!

Correct rule: ac≡bc(modn)ac \equiv bc \pmod{n} implies a≡b(modn/d)a \equiv b \pmod{n/d} where d=gcd⁡(c,n)d = \gcd(c, n).

If gcd⁡(c,n)=1\gcd(c, n) = 1, then we can divide: a≡b(modn)a \equiv b \pmod{n}.

Residue Classes

The residue class of aa modulo nn: [a]n={b∈Z:b≡a(modn)}[a]_n = \{b \in \mathbb{Z} : b \equiv a \pmod{n}\}

The set of residue classes: Zn={[0],[1],…,[n−1]}\mathbb{Z}_n = \{[0], [1], \ldots, [n-1]\}

We often write just 0,1,…,n−10, 1, \ldots, n-1 for convenience.

Modular Arithmetic Operations

Addition Table (mod 4)

+0123
00123
11230
22301
33012

Multiplication Table (mod 5)

×01234
000000
101234
202413
303142
404321

Modular Inverse

The modular inverse of aa modulo nn is an integer a−1a^{-1} such that: a⋅a−1≡1(modn)a \cdot a^{-1} \equiv 1 \pmod{n}

Existence

a−1a^{-1} exists if and only if gcd⁡(a,n)=1\gcd(a, n) = 1.

Finding the Inverse

Use the extended Euclidean algorithm to find xx such that: ax+ny=1ax + ny = 1 Then xx is the inverse of aa modulo nn.

Example

Find 7−1(mod11)7^{-1} \pmod{11}: 7⋅8=56=5⋅11+1≡1(mod11)7 \cdot 8 = 56 = 5 \cdot 11 + 1 \equiv 1 \pmod{11} So 7−1≡8(mod11)7^{-1} \equiv 8 \pmod{11}.

Linear Congruences

Solve: ax≡b(modn)ax \equiv b \pmod{n}

Solution

Let d=gcd⁡(a,n)d = \gcd(a, n).

No solution if d∤bd \nmid b.

If d∣bd \mid b: There are exactly dd solutions modulo nn.

Reduce to: adx≡bd(modnd)\frac{a}{d}x \equiv \frac{b}{d} \pmod{\frac{n}{d}}

Use modular inverse to solve.

Example

Solve 6x≡4(mod10)6x \equiv 4 \pmod{10}

gcd⁡(6,10)=2\gcd(6, 10) = 2 and 2∣42 \mid 4, so solutions exist.

Reduce: 3x≡2(mod5)3x \equiv 2 \pmod{5}

3−1≡2(mod5)3^{-1} \equiv 2 \pmod{5} (since 3⋅2=6≡13 \cdot 2 = 6 \equiv 1)

x≡2⋅2≡4(mod5)x \equiv 2 \cdot 2 \equiv 4 \pmod{5}

Solutions mod 10: x≡4x \equiv 4 or x≡9x \equiv 9.

Chinese Remainder Theorem

If n1,n2,…,nkn_1, n_2, \ldots, n_k are pairwise coprime, then the system: x≡a1(modn1)x \equiv a_1 \pmod{n_1} x≡a2(modn2)x \equiv a_2 \pmod{n_2} ⋮\vdots x≡ak(modnk)x \equiv a_k \pmod{n_k}

has a unique solution modulo N=n1n2⋯nkN = n_1 n_2 \cdots n_k.

Construction

x=∑i=1kaiNiyi(modN)x = \sum_{i=1}^{k} a_i N_i y_i \pmod{N}

where Ni=N/niN_i = N/n_i and yi=Ni−1(modni)y_i = N_i^{-1} \pmod{n_i}.

Example

Solve: x≡2(mod3)x \equiv 2 \pmod{3}, x≡3(mod5)x \equiv 3 \pmod{5}, x≡2(mod7)x \equiv 2 \pmod{7}

N=3⋅5⋅7=105N = 3 \cdot 5 \cdot 7 = 105

N1=35N_1 = 35, y1=35−1≡2−1≡2(mod3)y_1 = 35^{-1} \equiv 2^{-1} \equiv 2 \pmod{3} N2=21N_2 = 21, y2=21−1≡1−1≡1(mod5)y_2 = 21^{-1} \equiv 1^{-1} \equiv 1 \pmod{5} N3=15N_3 = 15, y3=15−1≡1−1≡1(mod7)y_3 = 15^{-1} \equiv 1^{-1} \equiv 1 \pmod{7}

x=2⋅35⋅2+3⋅21⋅1+2⋅15⋅1=140+63+30=233≡23(mod105)x = 2 \cdot 35 \cdot 2 + 3 \cdot 21 \cdot 1 + 2 \cdot 15 \cdot 1 = 140 + 63 + 30 = 233 \equiv 23 \pmod{105}

Fast Modular Exponentiation

Compute ak(modn)a^k \pmod{n} efficiently using binary representation of kk.

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(log⁡k)O(\log k) multiplications.

Example

313(mod7)3^{13} \pmod{7}

13=1101213 = 1101_2

31=33^1 = 3 32=9≡23^2 = 9 \equiv 2 34≡22=43^4 \equiv 2^2 = 4 38≡42=16≡23^8 \equiv 4^2 = 16 \equiv 2

313=38⋅34⋅31≡2⋅4⋅3=24≡3(mod7)3^{13} = 3^8 \cdot 3^4 \cdot 3^1 \equiv 2 \cdot 4 \cdot 3 = 24 \equiv 3 \pmod{7}