TExcelTAD

Congruences

Gauss's notation a≡b(modn)a \equiv b \pmod{n} turns "has the same remainder as" into something you can add, subtract and multiply like an equation. Clocks, check digits, hash functions and all of public-key cryptography are written in it.

Definition

Let n≥1n \ge 1. Integers aa and bb are congruent modulo nn,

a≡b(modn)  ⟺  n∣a−ba \equiv b \pmod{n} \iff n \mid a - b

equivalently, aa and bb leave the same remainder when divided by nn. For example 38≡14(mod12)38 \equiv 14 \pmod{12} (38 o'clock is 2 o'clock, and so is 14 o'clock), and −1≡6(mod7)-1 \equiv 6 \pmod{7}.

Congruence modulo nn is an equivalence relation (reflexive, symmetric, transitive), and it splits Z\mathbb{Z} into nn classes, one per remainder 0,1,…,n−10, 1, \ldots, n - 1.

python · try it yourself
def congruent(a, b, n):
    return (a - b) % n == 0

print(congruent(38, 14, 12), congruent(-1, 6, 7), congruent(100, 1, 9), congruent(10, 3, 4))
print("class of 3 mod 7:", [k for k in range(-20, 30) if k % 7 == 3])
True True True False
class of 3 mod 7: [-18, -11, -4, 3, 10, 17, 24]

Arithmetic with congruences

If a≡a′a \equiv a' and b≡b′(modn)b \equiv b' \pmod{n}, then

a+b≡a′+b′,a−b≡a′−b′,ab≡a′b′(modn)a + b \equiv a' + b', \qquad a - b \equiv a' - b', \qquad ab \equiv a'b' \pmod{n}

and so ak≡a′ka^k \equiv a'^k for every k≥0k \ge 0. Proof for products: ab−a′b′=a(b−b′)+b′(a−a′)ab - a'b' = a(b - b') + b'(a - a'), a sum of multiples of nn.

This is what makes modular arithmetic practical: you can reduce at every step and the numbers never grow. What is 7100 mod 67^{100} \bmod 6? Since 7≡17 \equiv 1, it is 1100=11^{100} = 1.

Division is the exception

You cannot cancel a common factor in general: 2⋅3≡2⋅6(mod6)2 \cdot 3 \equiv 2 \cdot 6 \pmod{6} but 3≢6(mod6)3 \not\equiv 6 \pmod 6. The rule is

ka≡kb(modn)  ⟹  a≡b(modn/gcd⁡(k,n))ka \equiv kb \pmod{n} \implies a \equiv b \pmod{n / \gcd(k, n)}

so cancellation is safe exactly when gcd⁡(k,n)=1\gcd(k, n) = 1. Division modulo nn means multiplying by a modular inverse.

python · try it yourself
n = 6
print([(a, b) for a in range(n) for b in range(n) if (2 * a - 2 * b) % n == 0 and a != b])
print("modulo 7, 2a = 2b only when a = b:", all((2 * a - 2 * b) % 7 or a == b for a in range(7) for b in range(7)))
[(0, 3), (1, 4), (2, 5), (3, 0), (4, 1), (5, 2)]
modulo 7, 2a = 2b only when a = b: True

Check digits

Many identifiers end in a digit chosen so that a weighted sum is ≡0\equiv 0 modulo some nn. The ISBN-10 rule is ∑i=110(11−i) di≡0(mod11)\sum_{i=1}^{10} (11 - i)\, d_i \equiv 0 \pmod{11} (an "X" stands for 10). Because 11 is prime, every single-digit error and every swap of two adjacent digits changes the sum modulo 11 and is detected.

python · try it yourself
def isbn10_ok(s):
    digits = [10 if c == "X" else int(c) for c in s if c not in "-"]
    return sum((10 - i) * d for i, d in enumerate(digits)) % 11 == 0

print(isbn10_ok("0-306-40615-2"))
print(isbn10_ok("0-306-40651-2"))      # two adjacent digits swapped
print(isbn10_ok("0-306-40615-3"))      # one digit wrong
True
False
False

In software

  • Hash tables put a key in bucket hash(key) % n; ring buffers advance with i = (i + 1) % n; angles in games wrap modulo 2π2\pi or 360.
  • The Luhn check digit on credit cards and the IBAN check (a 2-digit remainder modulo 97) are congruence checks.
  • Unsigned machine arithmetic is arithmetic modulo 2322^{32} or 2642^{64}: overflow is reduction.

Exercises

Exercise 1: Remainders of powers

Find 3100 mod 83^{100} \bmod 8 and 1050 mod 910^{50} \bmod 9 by hand.

Answer

1 and 1.

Solution

32=9≡1(mod8)3^2 = 9 \equiv 1 \pmod 8, so 3100=(32)50≡13^{100} = (3^2)^{50} \equiv 1. And 10≡1(mod9)10 \equiv 1 \pmod 9, so 1050≡110^{50} \equiv 1.

Exercise 2: Last digit

What is the last digit of 220262^{2026}?

Answer
Solution

Powers of 2 modulo 10 cycle with period 4 from 212^1: 2,4,8,6,2,…2, 4, 8, 6, 2, \ldots. Since 2026=4⋅506+22026 = 4 \cdot 506 + 2, 22026≡22=4(mod10)2^{2026} \equiv 2^2 = 4 \pmod{10}. Check with pow(2, 2026, 10).

Exercise 3: Cancellation (code)

Find all x∈{0,…,11}x \in \{0, \ldots, 11\} with 4x≡8(mod12)4x \equiv 8 \pmod{12} and explain the answer with the cancellation rule.

Answer

x=2,5,8,11x = 2, 5, 8, 11: all x≡2(mod3)x \equiv 2 \pmod 3.

Solution
python · try it yourself
print([x for x in range(12) if (4 * x - 8) % 12 == 0])
[2, 5, 8, 11]

gcd⁡(4,12)=4\gcd(4, 12) = 4, so cancelling 4 gives x≡2(mod12/4=3)x \equiv 2 \pmod{12/4 = 3}, four solutions modulo 12.

Test yourself

a≡b(modn)a \equiv b \pmod{n} means…

  1. a=ba = b
  2. n∣a−bn \mid a - b
  3. a∣ba \mid b
  4. a/b=na / b = n

Which operation is NOT always allowed on congruences modulo nn?

  1. adding the same number to both sides
  2. multiplying both sides by the same number
  3. dividing both sides by a common factor
  4. raising both sides to the same power

What is 123456789 mod 9123456789 \bmod 9?

  1. 0
  2. 1
  3. 8
  4. 9