Sign in

Libre University uses your GitHub account. Signing in is only needed to sit a final test, so the score is kept on your profile.

Modular arithmetic

To know what day of the week it will be in 1000 days there is no need to count them, because only the remainder of 1000 on division by 7 matters, and the problem is to turn that shortcut into arithmetic that can be trusted.

The previous lesson ended by noticing that divisibility questions are really questions about remainders. This lesson makes the remainders into a number system. It uses Bézout's identity from an earlier lesson: ax+by=gcd(a,b) always has an integer solution, found by running Euclid's algorithm backwards.

Congruence

Fix a positive integer n, the modulus. Two integers a and b are congruent modulo n, written

a≡b(modn)

when n∣a-b. So 17≡2(mod5), since 17-2=15, and -17≡3(mod5), since -17-3=-20. This is the same as leaving the same remainder: if a=nq+r and b=nq′+r′ with 0≤r,r′<n, then a-b=n(q-q′)+(r-r′), and r-r′ lies strictly between -n and n, so n∣a-b exactly when r=r′.

Proof and Logic showed that congruence modulo n is an equivalence relation, so it splits the integers into classes. Write [a] for the class of a, the set of all integers congruent to it. Modulo 5 the class [2] is {…,-8,-3,2,7,12,…}, and there are exactly five classes, [0],[1],[2],[3],[4], one for each possible remainder. The set of the n classes is written ℤn.

A class has many names: [2], [7] and [-3] are the same class modulo 5. That freedom lets a large number be swapped for a small one, and it is also a danger, because anything defined through a representative must come out the same whichever one is chosen.

Adding and multiplying classes

The natural definitions are [a]+[b]=[a+b] and [a][b]=[ab], and they are only well defined if the answer does not depend on which names were picked for the inputs.

Theorem. If a≡a′ and b≡b′(modn), then a+b≡a′+b′ and ab≡a′b′(modn).

For the sum, (a+b)-(a′+b′)=(a-a′)+(b-b′), a sum of two multiples of n. For the product, add and subtract ab′:

ab-a′b′=a(b-b′)+b′(a-a′)

and both terms on the right are multiples of n. So ℤn has an addition and a multiplication, which inherit the commutative, associative and distributive laws from the integers. Repeating the product rule gives ak≡bk(modn), so any calculation built from sums and products may be reduced at any stage.

The days of the week are ℤ7. A year of 365 days is 52 weeks and 1 day, so 365≡1(mod7) and a date moves one weekday later each ordinary year, and two after a leap day. The question in the first line is now easy: 1000=7×142+6, so 1000≡6≡-1(mod7), and 1000 days after a Sunday is a Saturday.

Example. Apollo 11 landed on the Moon on Sunday 20 July 1969. On what day of the week did the fiftieth anniversary, 20 July 2019, fall?

The fifty years contain the leap days of 1972, 1976 and so on to 2016, which is twelve of them (2000 was a leap year). So the gap is 50×365+12 days. Modulo 7, replace 365 by 1:

50×365+12≡50+12=62=7×8+6≡6(mod7)

Six days after Sunday is Saturday, which a computer's calendar confirms.

Now you. The United States Declaration of Independence is dated Thursday 4 July 1776. On what day of the week does its 250th anniversary, 4 July 2026, fall? Remember that in the Gregorian calendar 1800 and 1900 were not leap years, while 2000 was.

Answer

The leap years from 1780 to 2024 number 2024-17804+1=62, less 1800 and 1900, so 60 leap days. The gap is 250×365+60≡250+60=310≡2(mod7), since 308=7×44. Two days after Thursday is Saturday.

Divisibility tests from 10≡1 and 10≡-1

A number written in decimal with digits dk…d1d0 is d0+10d1+102d2+⋯+10kdk. Since 10≡1(mod9), every power 10j≡1j=1, and the whole number is congruent to d0+d1+⋯+dk. So a number and its digit sum leave the same remainder on division by 9, and by 3 too, since also 10≡1(mod3). The digits of 123456789 sum to 45, a multiple of 9, so 9 divides it.

Modulo 11, instead, 10≡-1, so 10j≡(-1)j, and a number is congruent modulo 11 to its alternating digit sum d0-d1+d2-⋯, starting from the units. For 918082 this is 2-8+0-8+1-9=-22, so 11 divides it, and indeed 918082=11×83462.

Both tests give more than a yes or no: they give the remainder. The alternating sum of 123456789 is 9-8+7-6+5-4+3-2+1=5, so it leaves remainder 5 on division by 11.

Inverses and zero divisors

Addition in ℤn can always be undone, since [-a] cancels [a]. Multiplication is another matter. A class [a] has a multiplicative inverse if some [x] has [a][x]=[1], that is ax≡1(modn). Modulo 7 every non-zero class has one: 2×4=8, 3×5=15 and 6×6=36 are all ≡1. Modulo 10 only 1,3,7,9 do.

Theorem. [a] has an inverse modulo n exactly when gcd(a,n)=1.

The congruence ax≡1(modn) says ax-1=ny for some integer y, that is ax+n(-y)=1. So an inverse exists exactly when 1 is an integer combination of a and n. If gcd(a,n)=1, Bézout's identity supplies the combination. If gcd(a,n)=g>1, then g divides every combination ax+ny and cannot divide 1. The proof is also the method: the x from extended Euclid is the inverse. An earlier lesson found 13×97-36×35=1, which read modulo 97 says 35×(-36)≡1, so the inverse of 35 is [-36]=[61]. Check: 35×61=2135=22×97+1.

When n=rs is composite, with 1<r,s<n, then [r][s]=[0] although neither factor is [0], as in 2×5≡0(mod10). A non-zero class that multiplies some non-zero class to [0] is a zero divisor, and it has no inverse: if ra≡0 with a not congruent to 0 and xr≡1, then a≡xra≡0. In fact, if gcd(a,n)=g>1 then a×ng=ag×n≡0, so every non-zero class is either invertible or a zero divisor, never both.

This is why cancellation is unsafe: 2×3≡2×8(mod10), yet 3 and 8 are not congruent modulo 10. Cancelling a is multiplying by its inverse, so it is allowed exactly when gcd(a,n)=1. Modulo a prime p every non-zero class is coprime to p and so invertible: ℤp allows division by anything non-zero, like the rationals. That is Euclid's lemma from the previous lesson in new clothes: modulo a prime, a product of non-zero classes is never zero.

Solving ax≡b(modn)

A linear congruence ax≡b(modn) says ax-ny=b for some integer y, so it is solvable exactly when b is a combination of a and n, which by Bézout means exactly when g=gcd(a,n) divides b. When g=1 there is one solution class, x≡a-1b. When g>1 and g∣b, write a=ga′, b=gb′, n=gn′. Then n∣ax-b exactly when n′∣a′x-b′, and since gcd(a′,n′)=1 that has one solution x0 modulo n′. Modulo n it splits into the g classes x0,x0+n′,…,x0+(g-1)n′.

Example. Solve 28x≡12(mod100).

Here gcd(28,100)=4, which divides 12, so there are four solutions modulo 100. Dividing through gives 7x≡3(mod25). For the inverse of 7 run Euclid: 25=3×7+4, 7=1×4+3, 4=1×3+1. Backwards, 1=4-3=2×4-7=2×25-7×7, so 7-1≡-7≡18(mod25). Then x≡18×3=54≡4(mod25), and modulo 100 the solutions are 4,29,54,79. Check one: 28×29=812≡12. By contrast 28x≡10(mod100) has no solution, since 4 does not divide 10.

Now you. Solve 21x≡15(mod57).

Answer

gcd(21,57)=3 divides 15, so there are three solutions. Dividing by 3 gives 7x≡5(mod19). Since 7×11=77=4×19+1, the inverse of 7 is 11, and x≡55≡17(mod19). Modulo 57: x≡17,36,55. Check: 21×17=357=6×57+15.

The Chinese remainder theorem

The Sunzi Suanjing, a Chinese arithmetic manual written between the third and fifth centuries AD, asks: there are things whose number is unknown; counted by threes, two are left; by fives, three are left; by sevens, two are left. How many things? In congruences, find x with x≡2(mod3), x≡3(mod5) and x≡2(mod7).

Theorem. If n1,…,nk are pairwise coprime and N=n1n2⋯nk, then for any a1,…,ak the system x≡ai(modni) has a solution, and it is unique modulo N.

The proof builds the solution. Let Ni=Nni, the product of the other moduli. It is coprime to ni, since a prime dividing both would divide some nj with j≠i as well as ni. So Ni has an inverse Mi modulo ni. Put

x=a1N1M1+a2N2M2+⋯+akNkMk

Modulo ni, every term except the ith vanishes, since its Nj contains the factor ni, while NiMi≡1, so x≡ai. For uniqueness, if x and x′ both work, then every ni divides x-x′. The ni share no prime, so by unique factorisation their product N divides x-x′ too.

Example. Solve Sunzi's problem.

N=105. For n1=3: N1=35≡2(mod3), whose inverse is 2. For n2=5: N2=21≡1, inverse 1. For n3=7: N3=15≡1, inverse 1. So

x=2×35×2+3×21×1+2×15×1=140+63+30=233

and 233-2×105=23. Check: 23=7×3+2=4×5+3=3×7+2. So the answer is 23, as the Sunzi Suanjing says, or any 23+105t.

Now you. Find the smallest positive x with x≡3(mod5), x≡4(mod7) and x≡5(mod11).

Answer

N=385. N1=77≡2(mod5), inverse 3; N2=55≡6(mod7), inverse 6; N3=35≡2(mod11), inverse 6. Then x=3×77×3+4×55×6+5×35×6=693+1320+1050=3063, and 3063-7×385=368. Check: 368=73×5+3=52×7+4=33×11+5.

Coprimality matters. The system x≡1(mod4), x≡2(mod6) has no solution, since the first makes x odd and the second even. The theorem also counts: the N classes modulo N and the N lists of remainders correspond one to one, so a number modulo 105 is the same information as its three remainders modulo 3, 5 and 7.

The ISBN check digit

Ten-digit International Standard Book Numbers, standardised in 1970, end in a check digit chosen so that, with the digits d1,…,d10 weighted 10 down to 1,

10d1+9d2+8d3+⋯+2d9+d10≡0(mod11)

The second edition of Concrete Mathematics by Graham, Knuth and Patashnik has ISBN 0-201-55802-5, and the weighted sum is 0+18+0+7+30+25+32+0+4+5=121=112. A check digit of 10 is printed as X.

A single wrong digit is always caught. Changing the digit with weight w from d to d′ changes the sum by w(d′-d), where 1≤w≤10 and d′-d is non-zero and at most 10 in size. Neither factor is a multiple of the prime 11, so by Euclid's lemma the product is not either, and the sum stops being ≡0. Swapping two adjacent digits a and b, with weights w and w-1, changes the sum by wb+(w-1)a-wa-(w-1)b=b-a, again non-zero modulo 11 when a≠b.

A composite modulus has zero divisors, and they let errors through. The thirteen-digit ISBN that replaced ISBN-10 on 1 January 2007 works modulo 10 with weights alternating 1 and 3, so swapping adjacent digits that differ by 5 changes the sum by 2×5≡0 and goes unnoticed.

Powers repeat

Take powers of 3 modulo 7: 3,2,6,4,5,1, and then, since 36≡1, the list starts again. Powers of 2 go 2,4,1 and repeat after three steps. Every sequence of powers modulo n must eventually repeat, because there are only n classes, but these return to 1, and the lengths 6 and 3 both divide 6=7-1. The next lesson explains the pattern with Fermat's little theorem and Euler's generalisation, computes huge powers by repeated squaring, and uses the inverses found here to build RSA, a lock anyone can close and only one person can open.