Recall the Division Theorem; for any integers a and b with b=0, there exist unique integers q and r such that
a=qb+r,0≤r<∣b∣.
In a lot of situations we don't actually care about the quotient q at all; the remainder r is the interesting part, so we give it its own operator.
Note
Definition
The modulo operatormod returns the canonical remainder when one integer is divided by another. Given integers a and b with b=0, we write amodb, read as "a modulo b", to mean the smallest non-negative remainder when a is divided by b. That is,
amodb=r, where a=qb+r and 0≤r<∣b∣ for some q,r∈Z.
Basically, amodb is just "the remainder when you divide a by b"; we throw away the quotient and keep only the leftover part. Notice that amodb=0 if and only if b∣a, since a remainder of zero means a is a perfect multiple of b.
In most programming languages this operator is written as %, but that symbol is never used for this purpose in actual mathematics.
Example. Find 19mod4, −11mod5 and 333mod3.
For each one, write out the Division Theorem and read off the remainder;
It is important to remember that the remainder is always non-negative, even when a is negative. It is tempting to say −11mod5=−1, but −1 is not a valid remainder; instead we push the quotient one lower (−3 rather than −2) so that the leftover becomes +4. Therefore, 19mod4=3, −11mod5=4 and 333mod3=0.
We just saw that 19mod4=3, and of course there are infinitely many integers x satisfying xmod4=3 (namely …,−5,−1,3,7,11,…,19,…,47,…). All of these numbers have something in common, so we say they belong to the same equivalence class. Instead of writing something clunky like 19mod4=47mod4, we use a special congruence notation, 19≡47(mod4).
Note
Definition
Given integers a and b and a positive integer m, we say that a and b are congruent modulo m and write
a≡b(modm)
to mean that amodm=bmodm.
Basically, two numbers are congruent modulo m if they land on the same spot when you wrap the number line around a circle of circumference m; think of a clock, where 13 o'clock and 1 o'clock are the same thing because 13≡1(mod12).
Note
Theorem The following statements are all equivalent:
a≡b(modm).
amodm=bmodm.
a and b have the same remainder when divided by m.
a=b+mk for some integer k.
m∣(a−b).
Proof. Statements (1), (2) and (3) all say the same thing by definition, so it is enough to show that (3) ⇒ (4) ⇒ (5) ⇒ (3), looping back around.
(3) ⇒ (4): Suppose a and b leave the same remainder r; then by the Division Theorem,
aba−ba=q1m+r,=q2m+r,=(q1−q2)m, (subtracting the two equations),=b+mk, where k=q1−q2∈Z.
(4) ⇒ (5): If a=b+mk, then a−b=mk, which is precisely the definition of m∣(a−b).
(5) ⇒ (3): Suppose m∣(a−b), and write a=q1m+r1 and b=q2m+r2 where 0≤r1,r2<m. Then
a−b=(q1−q2)m+(r1−r2).
Since m divides both a−b and (q1−q2)m, it must also divide their difference r1−r2. But −m<r1−r2<m, and the only multiple of m strictly between −m and m is 0; hence r1=r2. ■
The last characterisation is by far the most useful one in practice:
a≡b(modm)⟺m∣(a−b).
Example. Is it true that 47≡19(mod4)?
Rather than computing both remainders separately, just subtract and test divisibility;
47−19=28=4×7,
so 4∣(47−19). Therefore, 47≡19(mod4). Notice how checking m∣(a−b) only needs one division, so it's usually the fastest way to verify a congruence.
Lemma Congruence modulo m is an equivalence relation on Z. That is, for all a,b,c∈Z:
(Reflexive) a≡a(modm).
(Symmetric) If a≡b(modm), then b≡a(modm).
(Transitive) If a≡b(modm) and b≡c(modm), then a≡c(modm).
Reflexivity and symmetry are pretty self explanatory so I'm not gonna write much;m∣(a−a)=0 since everything divides 0, and if m∣(a−b) then m∣−(a−b)=(b−a). For transitivity, we know m∣(a−b) and m∣(b−c), and since divisibility is preserved under sums,
mm∣(a−b)+(b−c)∣(a−c).
This is exactly why congruence behaves so much like ordinary equality; it slices Z up into m equivalence classes (one for each possible remainder), and ≡ acts like "=" between the classes.
If a≡b(modm), and k∈Z+ satisfies k∣m, then a≡b(modk).
If a≡b(modm) and c≡d(modm), then a+c≡b+d(modm).
If a≡b(modm), then a+k≡b+k(modm) for all k∈Z.
If a≡b(modm) and c≡d(modm), then ac≡bd(modm).
If a≡b(modm), then ak≡bk(modm) for all k∈Z.
If a≡b(modm), then ak≡bk(modmk) for all k∈Z+.
If ak≡bk(modmk) for some k∈Z+, then a≡b(modm).
If ak≡bk(modm) for some k∈Z, and gcd(m,k)=1, then a≡b(modm).
If a≡b(modm), then ak≡bk(modm) for all k∈Z+.
Basically, congruences can be added, subtracted and multiplied together just like ordinary equations, and both sides can be raised to a positive power (property 9 is just property 4 applied to itself k times). Property 1 says a congruence survives shrinking the modulus to any of its divisors; for instance 26≡14(mod12), and since 6∣12, we also get 26≡14(mod6) for free (both leave remainder 2).
The odd ones out are properties 7 and 8, which are the only two ways to cancel. You cannot freely divide both sides of a congruence; cancellation is only allowed if you also divide the modulus by the same factor (property 7), or if the factor being cancelled is coprime with the modulus (property 8).
We prove two of these; the rest follow from very similar arguments.
Proof (property 4). Since a≡b(modm) and c≡d(modm), we can write a=b+mk and c=d+mℓ for some k,ℓ∈Z. Then
Since bℓ+kd+mkℓ∈Z, we have m∣(ac−bd), and hence ac≡bd(modm). ■
Proof (property 8). Since ak≡bk(modm), we know that
mm∣(ak−bk)∣(a−b)k.
Because gcd(m,k)=1, we can write mx+ky=1 for some x,y∈Z (the gcd is always an integer linear combination, via the extended Euclidean algorithm). Multiplying through by a−b,
a−b=(a−b)mx+(a−b)ky.
Now m divides the first term (it contains a factor of m), and m divides the second term (since m∣(a−b)k). Therefore m∣(a−b), i.e. a≡b(modm). ■
Example. Find (123×456+789)mod11.
The whole point of properties 2 and 4 is that we can reduce every number before doing any arithmetic, instead of multiplying huge numbers together. Reducing each term modulo 11;
Therefore, (123×456+789)mod11=7; we never had to touch a number bigger than 18.
Example. It is true that 6≡2(mod4). Can we cancel the common factor of 2 to conclude that 3≡1(mod4)?
No! Checking directly,
3−14=2,∤2,
so 3≡1(mod4); naive cancelling produced a false statement. The problem is that gcd(2,4)=2=1, so property 8 does not apply. What we are allowed to do is property 7, dividing the two sides and the modulus by 2;
63≡2(mod4)≡1(mod2),
which is true, since 3 and 1 are both odd. Therefore, before cancelling a factor k from a congruence, always check gcd(m,k) first; if k shares a factor with the modulus, that factor must be divided out of the modulus too.
Since congruence modulo m behaves so much like equality, we can build an entire self-contained number system out of it.
Note
Definition
Given any positive integer m, the ring of integers modulo m is denoted Zm, and is the number system with elements {0,1,2,…,m−1} and addition and multiplication operations the same as in Z, except always reduced to the smallest non-negative remainder modulo m.
(Zm is also sometimes written as Z/mZ.)
Basically, Zm is clock arithmetic; only the m numbers 0 to m−1 exist, and everything else wraps around. We may write "a=b in Zm" to mean exactly the same thing as congruence modulo m. For example, the following statements are all valid:
19=3 in Z4.
19=−1=4 in Z5.
Working in Z7, we have 699=−1=6 (since 700=7×100).
Notice how useful the negative representatives are; being allowed to swap 699 for −1 in Z7 means we can trade a big ugly number for a tiny one, which makes taking powers almost trivial.
Example. Show that 4100 leaves a remainder of 1 when divided by 5.
Since 4≡−1(mod5), property 9 lets us replace the base 4 with −1 before taking the power, and (−1)100=1 because 100 is even. Here are three ways of writing the exact same proof:
4100mod5=(−1)100mod5=1mod5=1.
4100≡(−1)100≡1(mod5).
4100=(−1)100=1 in Z5.
Therefore, 4100mod5=1; all three notations are interchangeable, so use whichever is cleanest for the question at hand.
Finding large powers ak in Zm is difficult, because you are allowed to reduce the base a modulo m, but you can NEVER reduce the exponent k modulo m. As a quick counterexample, take 24 in Z3; reducing the exponent 4mod3=1 would suggest 24≡21(mod3), but
24=1621≡1(mod3),≡2(mod3),
which are clearly not the same. The exponent counts how many times we multiply; it is not itself a number living in Zm.
Instead, the trick is to hunt for a small power of a that reduces to something close to 0 in Zm (ideally 1 or −1), and then use the Division Theorem on the exponent;
If aj≡±1(modm) and k=qj+r, then ak=(aj)q⋅ar≡(±1)qar(modm).
Example. Find 71001mod12.
Try small powers of 7 in Z12;
72=49=4×12+1≡1(mod12),
so we found a power congruent to 1 almost immediately. Splitting the exponent as 1001=2×500+1,
71001=(72)500×71≡1500×7(mod12)≡7(mod12).
Therefore, 71001mod12=7.
Example. Find 121001mod7.
First reduce the base; 12≡5≡−2(mod7), and −2 is the smaller (in size) representative, so work with that. Trying small powers of −2,
(−2)2(−2)3=4≡4(mod7),=−8≡−1(mod7),
so the cube gets us to −1. Splitting the exponent as 1001=3×333+2,
Therefore, 121001mod7=3. Don't forget the very last step; the mod operator demands a remainder between 0 and 6, so −4 must be converted to 3 before you write down the final answer.
Example. Find 51001mod93.
Small powers of 5 don't immediately give ±1 here, but keep going;
Sometimes the "find aj≡±1" strategy is doomed from the start. If gcd(a,m)=1, then every power of a shares that common factor with m, so no power of a can ever be congruent to 1 or −1. In that case, look for a repeating cycle in the powers instead.
Example. Find 3103mod15.
Here gcd(3,15)=3=1, so every power of 3 is a multiple of 3 in Z15, and neither 1 nor 14 is; hunting for ±1 would be a waste of time. Instead, list the powers and wait for a repeat;
Since 35≡31, the powers cycle with period 4 from exponent 1 onwards; multiplying both sides by 3 repeatedly gives 3k+4≡3k(mod15) for all k≥1. So we can strip multiples of 4 off the exponent (as long as we stop before reaching 0);
3103≡3103−4×25(mod15)≡33(mod15)≡12(mod15).
Therefore, 3103mod15=12. Note the subtlety: the cycle starts at exponent 1, not 0 (30=1 is not part of the loop), so we reduce the exponent using the cycle we found, never by blindly taking 103mod4 and allowing exponent 0.
Alternate solution. We can also exploit property 6 from the properties of modular arithmetic. Write 3103=3×3102 and 15=3×5; then it is enough to find 3102 in Z5, where gcd(3,5)=1 so the usual trick works;
Now multiply both sides and the modulus by 3 (property 6 with k=3);
31023×31023103≡4(mod5)≡3×4(mod15)≡12(mod15).
Therefore, 3103mod15=12, agreeing with the first method. The decision here is worth remembering; when the base shares a factor with the modulus, either find the cycle directly, or factor that common divisor out and work in the smaller coprime modulus.
These kinds of questions rarely announce themselves as "modular arithmetic"; the skill is recognising that some quantity wraps around with a fixed period, and choosing the right modulus.
The last digit of a number is exactly its remainder modulo 10 (this drops straight out of the base 10 representation, since every higher digit is multiplied by a power of 10, and 10≡0(mod10)).
Example. Find the last digit of 72026.
We want 72026mod10. Hunting for small powers of 7 in Z10,
Days of the week repeat with period 7, so any "what day will it be" question is just arithmetic in Z7.
Example. Today is a Thursday. What day of the week will it be 1000 days from now?
Reduce the number of days modulo 7;
1000=142×7+6≡6(mod7).
So 1000 days is 142 complete weeks (which change nothing) plus 6 extra days, and counting six days on from Thursday lands on Wednesday. Even quicker, notice that 6≡−1(mod7); going forward 1000 days is the same as going back one day, i.e. the day before Thursday. Therefore, 1000 days from now it will be a Wednesday.
Since 10≡1(mod9), property 9 gives 10k≡1k≡1(mod9) for every k∈Z+; so in Z9, every number collapses down to the sum of its digits. This is exactly where the "divisible by 9 if its digit sum is" test comes from (and the same works modulo 3, since 10≡1(mod3) too).
Example. Is 76543218 divisible by 9?
Expanding in base 10 and reducing each power of 10 to 1,
A classic exam trick is to rule something out by reducing modulo a small number and checking which residues are even possible.
Example. Show that 218745983 is not a perfect square.
Work in Z4. Every integer n is congruent to one of 0,1,2,3 modulo 4, so by property 9 there are only four cases for n2;
So every perfect square is congruent to 0 or 1 modulo 4; a square can never be 2 or 3 in Z4. Now, since 100≡0(mod4), any number is congruent modulo 4 to just its last two digits, hence
218745983≡83(mod4)≡3(mod4), (as 83=20×4+3).
Since 3 is not an achievable residue for a square, 218745983 cannot be a perfect square. Therefore, we ruled it out without computing a single square root; picking a small modulus and eliminating impossible residues is often far faster than direct computation.