Congruences
Gauss's notation 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 . Integers and are congruent modulo ,
equivalently, and leave the same remainder when divided by . For example (38 o'clock is 2 o'clock, and so is 14 o'clock), and .
Congruence modulo is an equivalence relation (reflexive, symmetric, transitive), and it splits into classes, one per remainder .
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 and , then
and so for every . Proof for products: , a sum of multiples of .
This is what makes modular arithmetic practical: you can reduce at every step and the numbers never grow. What is ? Since , it is .
Division is the exception
You cannot cancel a common factor in general: but . The rule is
so cancellation is safe exactly when . Division modulo means multiplying by a modular inverse.
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 modulo some . The ISBN-10 rule is (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.
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 wrongTrue
False
False
In software
- Hash tables put a key in bucket
hash(key) % n; ring buffers advance withi = (i + 1) % n; angles in games wrap modulo 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 or : overflow is reduction.
Exercises
Exercise 1: Remainders of powers
Find and by hand.
Answer
1 and 1.
Solution
, so . And , so .
Exercise 2: Last digit
What is the last digit of ?
Answer
Solution
Powers of 2 modulo 10 cycle with period 4 from : . Since , . Check with pow(2, 2026, 10).
Exercise 3: Cancellation (code)
Find all with and explain the answer with the cancellation rule.
Answer
: all .
Solution
print([x for x in range(12) if (4 * x - 8) % 12 == 0])[2, 5, 8, 11]
, so cancelling 4 gives , four solutions modulo 12.
Test yourself
means…
Which operation is NOT always allowed on congruences modulo ?
- adding the same number to both sides
- multiplying both sides by the same number
- dividing both sides by a common factor
- raising both sides to the same power
What is ?
- 0
- 1
- 8
- 9