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: ab(modn)    n(ab)a \equiv b \pmod{n} \iff n \mid (a - b)

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

Examples

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

Properties of Congruence

Equivalence Relation

  • Reflexive: aa(modn)a \equiv a \pmod{n}
  • Symmetric: If aba \equiv b, then bab \equiv a
  • Transitive: If aba \equiv b and bcb \equiv c, then aca \equiv c

Arithmetic Properties

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

a+cb+d(modn)a + c \equiv b + d \pmod{n} acbd(modn)a - c \equiv b - d \pmod{n} acbd(modn)ac \equiv bd \pmod{n} akbk(modn) for any k0a^k \equiv b^k \pmod{n} \text{ for any } k \geq 0

Division (Be Careful!)

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

Correct rule: acbc(modn)ac \equiv bc \pmod{n} implies ab(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: ab(modn)a \equiv b \pmod{n}.

Residue Classes

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

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

We often write just 0,1,,n10, 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 a1a^{-1} such that: aa11(modn)a \cdot a^{-1} \equiv 1 \pmod{n}

Existence

a1a^{-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 71(mod11)7^{-1} \pmod{11}: 78=56=511+11(mod11)7 \cdot 8 = 56 = 5 \cdot 11 + 1 \equiv 1 \pmod{11} So 718(mod11)7^{-1} \equiv 8 \pmod{11}.

Linear Congruences

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

Solution

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

No solution if dbd \nmid b.

If dbd \mid b: There are exactly dd solutions modulo nn.

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

Use modular inverse to solve.

Example

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

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

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

312(mod5)3^{-1} \equiv 2 \pmod{5} (since 32=613 \cdot 2 = 6 \equiv 1)

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

Solutions mod 10: x4x \equiv 4 or x9x \equiv 9.

Chinese Remainder Theorem

If n1,n2,,nkn_1, n_2, \ldots, n_k are pairwise coprime, then the system: xa1(modn1)x \equiv a_1 \pmod{n_1} xa2(modn2)x \equiv a_2 \pmod{n_2} \vdots xak(modnk)x \equiv a_k \pmod{n_k}

has a unique solution modulo N=n1n2nkN = 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=Ni1(modni)y_i = N_i^{-1} \pmod{n_i}.

Example

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

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

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

x=2352+3211+2151=140+63+30=23323(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(logk)O(\log k) multiplications.

Example

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

13=1101213 = 1101_2

31=33^1 = 3 32=923^2 = 9 \equiv 2 3422=43^4 \equiv 2^2 = 4 3842=1623^8 \equiv 4^2 = 16 \equiv 2

313=383431243=243(mod7)3^{13} = 3^8 \cdot 3^4 \cdot 3^1 \equiv 2 \cdot 4 \cdot 3 = 24 \equiv 3 \pmod{7}