Modular Arithmetic
Recall: for integers a and b with b > 0, we define q := a div b and r := a mod b so that r
.
To find what day of the week it will be in the future, you can do arithmetic modulo .
Depending on how your clock works, the hours might behave according to arithmetic modulo .
Congruence
We say that a is congruent to b modulo m, or a ≡ b (mod m), if m | b-a. Equivalently, this means that there exists an integer k such that
.
True or false: if a ≡ b (mod m) then b ≡ a (mod m).
Using congruence to simplify computations
Here are two useful facts about congruence:
- If
a ≡ b (mod m)andc ≡ d (mod m), thena+c ≡ b+d (mod m). - If
a ≡ b (mod m)andc ≡ d (mod m), thenac ≡ bd (mod m).
m by computing the remainders modulo m before doing any arithmetic.
a, b, and positive m,
(a + b) mod m = ((a mod m) + (b mod m)) mod m
(ab) mod m = ((a mod m)(b mod m)) mod m.
We can use this theorem to solve modular arithmetic problems with large inputs by reducing modulo m first, so that we don't have to deal with large intermediate quantities.
1234 x 5678 mod 10 = 4 x 8 mod 10 = 2.
77 x 78 mod 81 = (-4) x (-3) mod 81 = 12.
Try it yourself:
Note that the above theorem does not hold for exponentiation. Furthermore, we cannot always "cancel out" multiplicative factors modulo m.
c: m:
c: d:
m: