Dividing with remainder gives a way to find the greatest common divisor (Euclid's algorithm) and to write it as a combination of the two numbers (Bézout). That in turn decides when you can divide in clock arithmetic, proves unique factorization into primes, and powers RSA encryption and check digits.
Beyond the courseCS 240 (divides, gcd, prime, mod)Hammack, Book of Proof, ch. 5 and 11.3Velleman, How to Prove It, ch. 6Euclid, Elements, Book VII
Which day of the week is it 100 days from today? You divide 100 by 7 and keep the remainder. That is arithmetic where only remainders matter, and this chapter makes it rigorous. Two questions drive everything. When can you divide in such arithmetic, so that a rule like "3x≡4" has an answer? And what does the greatest common divisor, which you used in school to simplify fractions, really tell you? The answer to both is one identity of Bézout. From it come the factorization of every integer into primes, Fermat’s little theorem, the correctness of RSA encryption, and the check digits on books and bank cards.
Before you start
0.2: the well-ordering principle, induction and strong induction, and proof by contradiction. 0.4: equivalence relations and classes. Three quick checks.
Quick checkWhich of these sets is guaranteed by the well-ordering principle to have a smallest element?
The principle is about non-empty sets of positive integers. The second set is {8,9,10,…} with smallest element 8. The positive reals have no smallest element (x/2 is smaller), and the integers below 100 have no smallest element either.Review: well-ordering
Quick checkFor the relation a∼b iff 5∣(a−b), which of 3, 13, −2 are in the class [8]?
8−3=5, 8−13=−5 and 8−(−2)=10 are all multiples of 5, so all three are related to 8. Negative numbers and numbers larger than 8 are in the class too.Review: equivalence classes
Quick checkWhat is the remainder when −7 is divided by 3?
In this chapter remainders are always between 0 and n−1. We write −7=3⋅(−3)+2, so the quotient is −3 and the remainder is 2. The answer −1 comes from −7=3⋅(−2)−1, which has a negative remainder and is not allowed.
The picture
Take a week of seven days, numbered 0 (Sunday) to 6 (Saturday). Counting 100 days forward from a Wednesday (3) moves you 100mod7=2 places ahead, since 100=14⋅7+2. Friday. Only the remainder matters, because every full week returns you to the same day.
Now multiply. Here are the multiplication tables of the nonzero remainders, mod 7 and mod 6.
× mod 7
1
2
3
4
5
6
1
1
2
3
4
5
6
2
2
4
6
1
3
5
3
3
6
2
5
1
4
4
4
1
5
2
6
3
5
5
3
1
6
4
2
6
6
5
4
3
2
1
× mod 6
1
2
3
4
5
1
1
2
3
4
5
2
2
4
0
2
4
3
3
0
3
0
3
4
4
2
0
4
2
5
5
4
3
2
1
Look at the row of 3 in the first table: it contains 1 exactly once (3⋅5≡1), so 3 has an “inverse” 5 that undoes multiplication by 3. Every row of the mod 7 table is a shuffle of 1,…,6. In the mod 6 table the rows of 2, 3 and 4 have repeats and no 1: those numbers have no inverse, and 2⋅3≡0 even though neither factor is 0. The difference between 7 and 6 is that 7 is prime. The exact rule, for any modulus, is the main theorem of this chapter: a has an inverse mod n iff gcd(a,n)=1.
The idea, in words
Why· The chain of ideas
Everything follows from one picture. Dividing a by b with a remainder splits a into a multiple of b plus a leftover smaller than b. If a number d divides both a and b, it also divides the leftover, so the common divisors of (a,b) are the common divisors of (b,leftover), and the problem has become smaller. Repeating gives Euclid’s algorithm. Running the algorithm backwards writes the gcd as ax+by, which is Bézout’s identity. That identity is a way to divide: if gcd(a,n)=1 then ax+ny=1, so ax≡1(modn) and x is an inverse of a. Primes are the numbers with no common factor with anything smaller, so division always works mod a prime, and primes cannot be split, which gives unique factorization.
Where this came from· Euclid, Bachet and Bézout
Euclid’s Elements (around 300 BC) contains, in Book VII, the repeated-division method for finding the greatest common divisor of two numbers, and Proposition 30 of that book says that a prime dividing a product divides one of the factors, which is the key lemma below. The identity that the gcd is a combination ax+by was proved for integers by Claude Gaspar Bachet de Méziriac in the second edition of his collection of number puzzles (1624). Étienne Bézout, later in the eighteenth century, proved the corresponding statement for polynomials, and the integer statement came to carry his name. The problem all of them worked on is still the right one to keep in mind: given two sizes, which amounts can be measured out using only multiples of each?
Definitions and theorems
Division with remainder
Theorem BHammack 5.2
Division algorithm
Let a be an integer and b a positive integer. Then there exist unique integers q (the quotient) and r (the remainder) such that
a=bq+r,0≤r<b.
We write r=amodb.
In one lineFor any integer a and positive integer b there are unique integers q and r with a=bq+r and 0≤r<b.
Why it is trueSubtract multiples of b from a as long as the result stays non-negative; the smallest non-negative result is the remainder.
Proofstandard
The idea: Existence: among all the non-negative numbers of the form a minus a multiple of b, take the smallest, using well-ordering. It must be smaller than b, or we could subtract b once more. Uniqueness: two remainders differ by a multiple of b, but they are both less than b apart.
Let S={a−bk:k∈Z,a−bk≥0}.
We consider every leftover a−bk we can get by subtracting multiples of b and that is not negative.
S is non-empty.
Take k=−∣a∣: then a−bk=a+b∣a∣≥a+∣a∣≥0, since b≥1.
So S has a least element; call it r=a−bq.
If 0∈S then 0 is the smallest possible element. Otherwise every element of S is a positive integer, and the well-ordering principle (0.2) applies.
Then r≥0 and a=bq+r.
By the definition of S, r≥0.
Suppose r≥b.
Proof by contradiction. If r≥b then r−b=a−b(q+1)≥0, so r−b∈S, and it is smaller than r. That contradicts r being the least element.
Hence r<b. This proves existence.
The assumption led to a contradiction.
So b divides r′−r.
Uniqueness: assume a=bq+r=bq′+r′ with 0≤r≤r′<b (swap the names if needed). Subtract: b(q−q′)=r′−r.
So r′=r, and then b(q−q′)=0 gives q=q′.
But 0≤r′−r<b, and the only multiple of b in that range is 0.
Worked example
Find the quotient and remainder when (a) 100 is divided by 7 and (b) −17 is divided by 5.
Solution.
(a) Find the largest multiple not above 100.7⋅14=98≤100<105=7⋅15. So q=14. The quotient is the number of full sevens that fit.
(a) The leftover.r=100−98=2. The remainder is a−bq, and it lies between 0 and 6.
(b) Negative number: go below, not above.5⋅(−4)=−20≤−17<−15=5⋅(−3). So q=−4. The remainder must be non-negative, so the multiple of 5 must not exceed −17. Choosing q=−3 would give r=−17+15=−2, which is negative.
(b) The leftover.r=−17−(−20)=3. −17=5⋅(−4)+3.
Answer. (a) 100=7⋅14+2. (b) −17=5⋅(−4)+3, so −17mod5=3.
Divisibility
Definition B
Divides
For integers a and b, adividesb, written a∣b, if there is an integer k with b=ak. We also say a is a divisor or factor of b and b is a multiple of a. When a does not divide b, we write a∤b. Note a∣0 for every a (take k=0), and 0∣b only if b=0.
In one linea∣b means b is a whole-number multiple of a: b=ak for some integer k.
Why it existsIt is the precise form of 'goes into evenly', and every statement about factors and remainders is built from it.
Proposition B
Divisibility facts
For integers a,b,c:
If a∣b and a∣c, then a∣(bx+cy) for all integers x,y.
If a∣b and b∣c, then a∣c.
If a∣b and b=0, then ∣a∣≤∣b∣.
In one lineDivisors pass through sums and multiples: if a divides b and c, it divides bx+cy for all integers x,y; divisibility chains; and a divisor of a non-zero number is at most as large in size.
Why it is trueEach is a one-line calculation from the definition b=ak.
Proofstandard
The idea: Write each hypothesis as an equation b = ak and substitute.
(1) Write b=ak and c=al.
Definition of divides, used for both hypotheses.
Then bx+cy=a(kx+ly), which is a multiple of a.
Substitute and factor out a. The bracket is an integer because sums and products of integers are integers.
Then c=(ak)l=a(kl), a multiple of a.
(2) Write b=ak and c=bl. Substitute.
Then ∣b∣=∣a∣∣k∣≥∣a∣.
(3) Write b=ak. Since b=0, k=0, and a non-zero integer has size at least 1.
The greatest common divisor
Definition B
Gcd
For integers a,b not both zero, gcd(a,b) is the largest positive integer that divides both a and b. It exists: 1 is a common divisor, and by fact 3 every common divisor is at most max(∣a∣,∣b∣) in size, so there are only finitely many. We say a and b are coprime (or relatively prime) if gcd(a,b)=1. For example gcd(12,18)=6, gcd(9,16)=1, and gcd(a,0)=∣a∣.
In one lineThe greatest common divisor of a and b is the largest positive integer that divides both.
Why it existsIt is the 'common measure' of two quantities, and the key to simplifying fractions and to dividing in modular arithmetic.
How do you compute a gcd when the numbers have hundreds of digits? Listing divisors is hopeless. Euclid’s insight is that one step of division makes the problem smaller without changing the answer.
Lemma B
Euclid's reduction step
If a,b,q,r are integers with a=bq+r and b=0, then gcd(a,b)=gcd(b,r) (where gcd(b,0)=∣b∣).
In one lineIf a=bq+r then a and b have exactly the same common divisors as b and r, so gcd(a,b)=gcd(b,r).
Why it is trueA number dividing a and b divides a−bq=r, and a number dividing b and r divides bq+r=a.
ProofEuclid VII.2
The idea: Show the two pairs have the same set of common divisors. Then they have the same largest element.
Let d divide both a and b. Then d∣a−bq=r, so d is a common divisor of b and r.
Fact 1 with x=1 and y=−q: a divisor of a and b divides a−qb.
Conversely let d divide both b and r. Then d∣bq+r=a, so d is a common divisor of a and b.
Fact 1 again: a divisor of b and r divides qb+r.
The pairs (a,b) and (b,r) have the same common divisors, so the same greatest common divisor.
Two sets that are equal have the same largest element.
Theorem B
Euclid's algorithm
For positive integers a,b, define r0=a, r1=b, and, as long as ri=0, divide: ri−1=qiri+ri+1 with 0≤ri+1<ri. Then the sequence reaches 0, and the last non-zero term rn equals gcd(a,b).
In one lineRepeat 'divide the larger by the smaller and keep the remainder' until the remainder is zero; the last non-zero remainder is the gcd.
Why it is trueEach division replaces the pair by a pair with the same gcd, and the numbers strictly shrink so the process must stop.
ProofEuclid VII.2
The idea: Termination: the remainders strictly decrease and cannot go below 0. Correctness: apply the reduction step at every line.
The sequence r1>r2>⋯≥0 must reach 0 after finitely many steps; say rn+1=0.
By the division algorithm each remainder satisfies 0≤ri+1<ri, so r1>r2>r3>…. A strictly decreasing sequence of non-negative integers has only finitely many terms (by well-ordering: its set of values has a least element).
Euclid's reduction step applied to each line ri−1=qiri+ri+1.
So gcd(a,b)=rn, the last non-zero remainder.
rn+1=0, and gcd(rn,0)=rn.
Worked example
Compute gcd(252,105) with Euclid's algorithm.
Solution.
Divide the larger by the smaller.252=2⋅105+42. 2⋅105=210≤252<315, leaving 252−210=42.
Replace the pair. Now work with (105,42): 105=2⋅42+21. The reduction step: gcd(252,105)=gcd(105,42).
Again.42=2⋅21+0. Remainder zero, so we stop. gcd(105,42)=gcd(42,21)=21.
Answer.gcd(252,105)=21. Check: 252=12⋅21 and 105=5⋅21, and 12 and 5 have no common factor.
Your answerUse Euclid's algorithm to find gcd(1547,560).
Divide the larger by the smaller, then repeat with the smaller number and the remainder, until the remainder is 0.
1547=2⋅560+427, 560=1⋅427+133, 427=3⋅133+28, 133=4⋅28+21, 28=1⋅21+7, 21=3⋅7+0. The last non-zero remainder is 7.
Bézout’s identity
Euclid’s algorithm finds the gcd. Running it backwards finds something more useful.
Theorem BBachet 1624; Hammack 5.3
Bézout's identity
If a,b are integers, not both zero, there exist integers x,y with ax+by=gcd(a,b). Moreover gcd(a,b) is the smallest positive integer that can be written as ax+by.
In one lineThe gcd of a and b is always an integer combination of them: gcd(a,b)=ax+by for some integers x,y. In fact it is the smallest positive number of that form.
Why it is trueLook at all positive numbers of the form ax+by. The smallest one must divide a and b, else dividing would produce a smaller one.
ProofHammack 5.3
The idea: Let d be the smallest positive combination ax + by (it exists by well-ordering). Show d divides both a and b by dividing a by d and seeing the remainder is a smaller combination, hence zero. Then d is a common divisor, and every common divisor divides d, so d is the greatest.
Let S={ax+by:x,y∈Z,ax+by>0}.
We study all the positive integers that can be built from a and b with integer coefficients.
S is non-empty, so it has a least element d=ax0+by0.
Not both of a,b are zero, so a⋅a+b⋅b>0 lies in S. Well-ordering (0.2) gives a least element.
Divide a by d: a=qd+r with 0≤r<d.
The division algorithm (above) applied to a and the positive number d.
r=a−qd=a(1−qx0)+b(−qy0).
Substitute d=ax0+by0: the remainder is again a combination of a and b.This is the key idea. A remainder from dividing by a combination is itself a combination.
So r=0, that is d∣a. The same argument gives d∣b.
If r>0 it would be a positive combination smaller than d, contradicting that d is the least element of S.
Therefore d=gcd(a,b).
Now d is a common divisor, so d≤gcd(a,b). Conversely gcd(a,b) divides a and b, so by fact 1 it divides ax0+by0=d, and by fact 3 it is at most d.
Proof workshopRebuild the proof of Bézout's identity
Tap the lines in the order a proof would write them. Each line can only use what is already above it.
Start by collecting all positive combinations and taking the smallest, which exists by the well-ordering principle.
The division algorithm, applied to a and the positive number d.
You need d to exist before you can divide by it.
Substitute the formula for d; the remainder is again built from a and b.
This substitutes the formula for d into the division equation, so the division must come first.
The least element cannot have a smaller element of S below it.
This needs the remainder written as a combination.
Divisors pass through combinations, and a divisor of a positive number is at most that number.
You first need d to be a combination, from the first line.
It is a common divisor, and no common divisor exceeds it.
This conclusion needs both that d is a common divisor and that nothing is larger.
Quick checkWhich of these integers can be written as 12x+18y with integer x,y?
gcd(12,18)=6. Every combination 12x+18y is a multiple of 6, because 6 divides both terms. Conversely every multiple of 6 is a combination: 6=12⋅(−1)+18⋅1, and scaling gives 30=12⋅(−5)+18⋅5. Neither 5 nor 7 is a multiple of 6.
Corollary B
Linear combinations of two integers
Let g=gcd(a,b). The equation ax+by=c has an integer solution if and only if g∣c.
In one lineax+by=c has an integer solution (x,y) exactly when gcd(a,b) divides c. In particular a and b are coprime iff ax+by=1 is solvable.
Why it is trueEvery combination is a multiple of the gcd, and Bézout gives all multiples.
Proofstandard
The idea: One direction: g divides both terms. The other: scale the Bézout combination.
If ax+by=c, then g∣c.
Fact 1: g divides a and b, hence divides ax+by.
Conversely, write c=gk and take ax0+by0=g.
If g∣c then c=gk. Bézout gives ax0+by0=g.
Then a(kx0)+b(ky0)=gk=c, so (kx0,ky0) is a solution.
Multiply the Bézout identity by k.
How do you find x and y? Run Euclid’s algorithm, then substitute backwards. This is the extended Euclidean algorithm.
Python labThe extended Euclidean algorithm, run by a computer
def ext_gcd(a, b): """Return (g, x, y) with g = gcd(a, b) = a*x + b*y.""" if b == 0: return a, 1, 0 q, r = divmod(a, b) # a = q*b + r g, x, y = ext_gcd(b, r) # g = b*x + r*y return g, y, x - q*y # substitute r = a - q*bg, x, y = ext_gcd(240, 46)print(f"gcd = {g} = 240*({x}) + 46*({y})")print("check:", 240*x + 46*y)def inverse(a, n): g, x, _ = ext_gcd(a, n) if g != 1: return None # no inverse unless gcd(a, n) = 1 return x % nprint("inverse of 11 mod 26:", inverse(11, 26))
Try this.Check the trace above with ext_gcd(240, 46). Then find the inverse of 11 mod 26 (the answer should be 19), and try two large numbers such as ext_gcd(261 - 1, 1018). The number of steps grows only like the number of digits, which is why RSA can use this on numbers with hundreds of digits.
Worked example
Solve 240x+46y=14 in integers, and decide whether 240x+46y=15 is solvable.
Solution.
Compute the gcd. From the trace above, gcd(240,46)=2. By the corollary, solvability depends only on whether the gcd divides the right side.
Test 14.2∣14, so a solution exists. The corollary, “if” direction.
Scale the Bézout identity. From 240(−9)+46(47)=2, multiply by 7: 240(−63)+46(329)=14. 14=7⋅2. Check: −15120+15134=14.
More solutions. Replacing (x,y) by (x+23t,y−120t) keeps the value, since 240⋅23=5520=46⋅120. Adding 23 copies of 240 and subtracting 120 copies of 46 changes the total by 5520−5520=0.
Test 15.2∤15, so there is no solution. Every value 240x+46y is even.
Answer.(x,y)=(−63,329) solves the first equation, and the second has no integer solution.
Primes and unique factorization
Definition B
Prime
An integer p>1 is prime if its only positive divisors are 1 and p. An integer n>1 that is not prime is composite: n=ab with 1<a,b<n. The number 1 is neither. The first primes are 2,3,5,7,11,13,…
In one lineA prime is an integer greater than 1 whose only positive divisors are 1 and itself.
Why it existsPrimes are the atoms of the integers: every integer above 1 is built by multiplying primes.
The next result feels familiar, and you used it in JEE without a thought: if a prime divides a product, it divides one of the factors. It needs a proof, because it fails for composites (6∣2⋅3 but 6 divides neither 2 nor 3). It needs Bézout.
Lemma BEuclid VII.30
Euclid's lemma
Let p be prime and a,b integers. If p∣ab, then p∣a or p∣b. More generally, if p∣a1a2⋯ak then p∣ai for some i.
In one lineIf a prime p divides a product ab, then p divides a or p divides b.
Why it is trueIf p does not divide a, then p and a are coprime, so Bézout gives 1=px+ay, and multiplying by b shows p∣b.
ProofHammack 5.4
The idea: Assume p does not divide a, and show p must divide b. The gcd of p and a is 1 because p has only two divisors. Then Bézout gives a combination equal to 1, which turns the hypothesis into the conclusion.
Suppose p∤a. We show p∣b.
Proof by cases (0.2): either p∣a, and we are done, or p∤a. We study the second case.
So gcd(p,a)=1.
gcd(p,a) is a positive divisor of p, so it is 1 or p. It is not p, since p∤a.
Choose integers x,y with px+ay=1.
Bézout's identity.
Then pbx+aby=b.
Multiply both sides by b.
Both terms on the left are multiples of p.
p divides pbx because pbx=p(bx), and p divides ab by hypothesis, so p divides aby.
So p∣b.
Fact 1: a divisor of two numbers divides their sum.
The version for k factors follows by induction on k.
For k factors, induct on k: apply the two-factor case to a1 and the product a2⋯ak.
Proof workshopRebuild the proof of Euclid's lemma
Tap the lines in the order a proof would write them. Each line can only use what is already above it.
Proof by cases: if p∣a there is nothing to prove, so we handle the remaining case.
A prime has only two positive divisors, and p is ruled out by the assumption.
This uses the assumption p∤a, which is stated first.
The gcd is a combination of the two numbers.
Bézout needs the gcd to be known, which is the previous line.
We want b alone on one side, so we scale the Bézout equation by b.
This scales the Bézout equation, which must be written first.
The first term has p as a factor. The second term is a multiple of ab, which p divides by hypothesis.
This looks at the two terms of the equation from the previous line.
A divisor of two numbers divides their sum.
The conclusion needs that both terms are multiples of p.
Worked example Hammack 5.4
Prove: if a∣bc and gcd(a,b)=1, then a∣c.
Solution. The plan: this is Euclid’s lemma with a not required to be prime. Use Bézout to get a combination equal to 1, then multiply by c.
Use Bézout. Since gcd(a,b)=1, there are integers x,y with ax+by=1. The coprime hypothesis is only usable through Bézout.
Multiply by c.acx+bcy=c. We want c alone on one side, because the goal is to show a divides c.
Check both terms.a∣acx because it has the factor a. a∣bcy because a∣bc by hypothesis. The first term is a multiple of a by its shape, the second by the hypothesis.
Add. So a divides acx+bcy=c. A divisor of two numbers divides their sum (fact 1).
Answer.a∣c. For example, 6∣4⋅9 does not give 6∣9, because gcd(6,4)=2=1.
Quick check6∣4⋅9=36, but 6∤4 and 6∤9. Why does this not contradict Euclid's lemma?
The lemma is about a PRIME divisor. 6=2⋅3 can split its prime factors between the two numbers (2∣4 and 3∣9), so it divides the product without dividing either factor. A prime cannot be split.
Theorem BEuclid IX.14; Gauss 1801
Fundamental theorem of arithmetic
Every integer n>1 can be written as a product of primes n=p1p2⋯pr (a prime is a product of one prime). If p1⋯pr=q1⋯qs with all pi,qj prime, then r=s and the qj are the pi in some order.
In one lineEvery integer greater than 1 is a product of primes, in exactly one way apart from the order of the factors.
Why it is trueExistence: split a composite number into smaller ones until the pieces are prime. Uniqueness: Euclid's lemma lets you match primes from two factorizations one at a time.
ProofHammack 10.3
The idea: Existence by strong induction: a composite number is a product of two smaller numbers, each of which is a product of primes by the hypothesis. Uniqueness by induction on r: the first prime p must divide some q_j by Euclid's lemma, hence equals it; cancel and repeat.
S(2) holds, since 2 is prime. Let n>2 and assume S(k) for 2≤k<n.
Existence, by strong induction on n (0.2). Let S(n) say that n is a product of primes. Assume S(k) for all 2≤k<n.
Case 1: n is prime. Done.
If n is prime it is a product of one prime, so S(n) holds.
Case 2: n=ab with 2≤a,b<n.
Case 2: n is not prime, so n=ab with 1<a,b<n (a divisor other than 1 and n; the other factor b=n/a is also between 1 and n by fact 3).
Both a and b are products of primes, so n=ab is a product of primes.
The strong induction hypothesis applies to a and to b.
Take the last prime on the left, pr. It divides some qj.
Uniqueness, by induction on r. Suppose p1⋯pr=q1⋯qs. Then pr divides q1⋯qs, so by Euclid's lemma pr∣qj for some j.
So pr=qj.
qj is prime, so its only positive divisors are 1 and qj. And pr>1.
Then p1⋯pr−1=q1⋯qs−1.
Renumber so that j=s, and cancel the common factor pr=qs from both sides (it is non-zero).
If r=1, then s=1 and the two factorizations are the same single prime.
Case r=1. The left side is now the empty product 1. A product of one or more primes is at least 2, so the right side must be empty too: s=1. Then q1=p1 and we are done.
If r≥2, then r−1=s−1 and q1,…,qs−1 are p1,…,pr−1 in some order. Put back pr=qs: r=s and the lists agree up to order.
Case r≥2. The left side is at least 2, so the right side is not empty and s−1≥1. Both sides are now factorizations with r−1 and s−1 primes, so the induction hypothesis (the theorem for r−1 primes on the left) applies.
Worked example
Factor 360 and 84 into primes, and find their gcd and lcm from the factorizations. Check with Euclid.
Solution.
Factor.360=23⋅32⋅5 and 84=22⋅3⋅7. Divide out the smallest prime repeatedly: 360=8⋅45=8⋅9⋅5. By the theorem, this is the only factorization.
The gcd takes the smaller exponent of each prime.2min(3,2)⋅3min(2,1)⋅5min(1,0)⋅7min(0,1)=22⋅3=12. A common divisor can use each prime only as often as it appears in both numbers, and uniqueness of factorization is what makes “how often a prime appears” well defined.
The lcm takes the larger exponent.23⋅32⋅5⋅7=2520. The least common multiple needs each prime as often as it appears in either number.
Check with Euclid.360=4⋅84+24, 84=3⋅24+12, 24=2⋅12. So the gcd is 12. Two methods must agree. Also 12⋅2520=30240=360⋅84.
Answer.gcd=12 and lcm=2520. Factorizing is easy for small numbers, but for a 600-digit number nobody knows a fast method. Euclid’s algorithm, on the other hand, is fast for any size. That contrast is the basis of RSA below.
A preview· Preview: unique factorization is special
In the number system Z[−5] of numbers a+b−5 with a,b integers, the number 6 factors as 2⋅3 and also as (1+−5)(1−−5), and none of these four factors can be broken down further. Unique factorization fails there. What goes wrong is exactly the step “a prime dividing a product divides a factor”, the content of Euclid’s lemma. Rings where that survives are the subject of MATH 541.
Arithmetic mod n
Definition B
Congruent mod n
Fix a positive integer n (the modulus). Integers a and b are congruent mod n, written a≡b(modn), if n∣(a−b). This is the relation ∼n of 0.4, which was shown there to be an equivalence relation. Its classes are the residue classes[a]n={a+nk:k∈Z}.
In one linea≡b(modn) means n divides a−b: a and b leave the same remainder on division by n.
Why it existsIt is the precise form of 'the same up to whole multiples of n', the sameness rule behind clock arithmetic.
Theorem B
There are exactly n residue classes
For every integer a there is exactly one r∈{0,1,…,n−1} with a≡r(modn), namely r=amodn. Consequently the set of classes is Zn={[0],[1],…,[n−1]}, with exactly n elements.
In one lineEvery integer is congruent mod n to exactly one of 0,1,…,n−1, namely its remainder; so there are exactly n classes, and a≡b iff a and b have the same remainder.
Why it is trueThe division algorithm gives each integer a remainder in that range, and two different numbers in that range cannot differ by a multiple of n.
ProofHammack 11.3
The idea: Existence from the division algorithm. Uniqueness: two numbers in the range 0 to n minus 1 differ by less than n.
So a−r=nq, a multiple of n, and a≡r.
Division algorithm: a=nq+r with 0≤r<n.
Then n∣r−r′.
Uniqueness. Suppose a≡r and a≡r′ with 0≤r,r′<n. By symmetry and transitivity (it is an equivalence relation), r≡r′, that is n∣(r−r′).
So r=r′.
∣r−r′∣<n, and the only multiple of n of size less than n is 0 (fact 3).
The classes [0],…,[n−1] are all different and every integer lies in one of them. This matches the ‘sorted into rows’ picture of chapter 0.4.
Equal classes are the same class. Each class contains exactly one remainder.
Now the point of building classes: we want to add and multiply them. Define [a]+[b]=[a+b] and [a][b]=[ab]. Just as for fractions in 0.4, this is only a legitimate definition if the answer does not depend on which representatives a,b you use.
Theorem B
Arithmetic respects congruence
Let a≡a′(modn) and b≡b′(modn). Then a+b≡a′+b′, a−b≡a′−b′, and ab≡a′b′ (mod n).
In one lineIf a≡a′ and b≡b′ (mod n), then a+b≡a′+b′ and ab≡a′b′; so addition and multiplication of classes are well defined.
Why it is trueReplacing a number by another in its class only adds multiples of n, and those stay multiples of n after adding or multiplying.
ProofHammack 11.3
The idea: Write a' = a + nk and b' = b + nl and expand. This is exactly the well-definedness check of chapter 0.4.
Write a′=a+nk and b′=b+nl for integers k,l.
Definition of congruence.
(a′+b′)−(a+b)=n(k+l), so a+b≡a′+b′.
Add the two equations. The difference is a multiple of n.
a′b′−ab=n(al+kb+nkl), a multiple of n, so ab≡a′b′.
Expand a′b′=(a+nk)(b+nl)=ab+n(al+kb+nkl).
(a′−b′)−(a−b)=n(k−l), so a−b≡a′−b′.
The same bookkeeping for the difference.
So Zn has addition and multiplication, and the usual laws (commutative, associative, distributive) hold because they hold in Z: for instance [a]+[b]=[a+b]=[b+a]=[b]+[a]. In practice we write a for [a], and compute with the smallest representatives. This is the “clock” or “calendar” arithmetic of the picture.
Worked example
Find (a) the last two digits of 72025, (b) 2100mod7, and (c) test whether 4725 is divisible by 9 and 918082 by 11.
Solution.
(a) Find a power that is 1.72=49 and 74=2401≡1(mod100). If some power is ≡1, then powers repeat with that period, so exponents can be reduced.
(a) Reduce the exponent.2025=4⋅506+1, so 72025=(74)506⋅7≡1506⋅7=7(mod100). Arithmetic respects congruence, so we may replace 74 by 1 inside the product.
(a) Answer. The last two digits are 07. The remainder mod 100 is the last two digits.
(b) Same method.23=8≡1(mod7) and 100=3⋅33+1, so 2100=(23)33⋅2≡2(mod7). The same pattern: find the power that gives 1.
(c) Digit rule for 9.10≡1(mod9), so 10k≡1 and a number with digits dk…d1d0 is congruent to dk+⋯+d0. For 4725: 4+7+2+5=18≡0, so 9∣4725 (indeed 4725=9⋅525). Congruence respects sums and products, so each d⋅10i≡d⋅1i.
(c) Digit rule for 11.10≡−1(mod11), so 10i≡(−1)i and a number is congruent to its digits with alternating signs from the right. For 918082: 2−8+0−8+1−9=−22≡0, so 11∣918082 (indeed 918082=11⋅83462). Same idea with −1 in place of 1.
Answer. (a) 07. (b) 2. (c) Both are divisible. In JEE you memorised these digit tests; they are congruences mod 9 and mod 11.
Watch out· You cannot always divide in modular arithmetic
Mod 6: 2⋅3≡2⋅6≡0, but 3≡6. So “cancel the 2” gives a wrong answer. Addition and multiplication respect congruence, but cancelling a factor needs that factor to be invertible, which is the next theorem.
Inverses mod n
Definition B
Inverse mod n
An integer b is an inverse of a mod n if ab≡1(modn). We write a−1 for it. In the mod 7 table, 3−1=5 because 3⋅5=15≡1.
In one lineAn inverse of a mod n is a number b with ab≡1(modn); it lets you divide by a.
Why it existsDividing by a is multiplying by its inverse, so inverses are what make equations ax≡c solvable.
Theorem BHammack 11.3
Inverses mod n exist exactly when coprime
For a positive integer n and an integer a: a has an inverse mod n iff gcd(a,n)=1. Any two inverses of a are congruent mod n.
In one linea has an inverse mod n if and only if gcd(a,n)=1; the inverse is unique mod n and is found by the extended Euclidean algorithm.
Why it is trueAn inverse is a solution of ax+ny=1 for x, which Bézout says exists exactly when the gcd is 1.
ProofHammack 11.3
The idea: An inverse b means ab minus 1 is a multiple of n, which is a Bézout equation. Bézout both ways.
So ab−nk=1.
(⇒) Suppose ab≡1. By the definition of congruence, ab−1=nk for some integer k.
Hence d∣1, so d=1 and gcd(a,n)=1.
Any common divisor d of a and n divides ab−nk (fact 1).
Choose x,y with ax+ny=1.
(⇐) Bézout's identity: gcd(a,n)=1 gives integers x,y with ax+ny=1.
So ax≡1(modn), and x is an inverse of a.
Then ax−1=−ny, a multiple of n.
Any two inverses are congruent mod n.
Uniqueness. If b and b′ are both inverses, b≡b(ab′)=(ba)b′≡b′. Each step uses that arithmetic respects congruence.
Quick checkWhich of 4, 9, 7 has an inverse mod 12?
gcd(4,12)=4 and gcd(9,12)=3, so neither is invertible. gcd(7,12)=1, and 7⋅7=49=4⋅12+1≡1, so 7 is its own inverse.
Your answerFind the inverse of 11 mod 26, as a number from 0 to 25.
Run the extended Euclid on 26 and 11 and read off the coefficient of 11.
Your answerHow many of the classes [0],[1],…,[35] in Z36 have an inverse mod 36?
Use the theorem: invertible iff coprime to 36=22⋅32.
[k] has an inverse iff gcd(k,36)=1, that is iff k is divisible by neither 2 nor 3. Among 0,…,35, half are odd (18), and of those a third are multiples of 3 (6 of them), leaving 18−6=12: the numbers 1,5,7,11,13,17,19,23,25,29,31,35.
Quick checkIn Z6 you are told 2x≡2y(mod6). What can you conclude?
gcd(2,6)=2=1, so 2 has no inverse mod 6 and cannot be cancelled freely. Writing 2x−2y=6k gives x−y=3k, so x≡y(mod3) and that is all you get. For example 2⋅1≡2⋅4(mod6) but 1≡4(mod6).
Worked example
Solve 7x≡4(mod40). Also explain why 4x≡1(mod6) has no solution.
Solution.
Check that 7 is invertible.gcd(7,40)=1. The theorem: invertible iff coprime.
Find the inverse with the extended Euclid.40=5⋅7+5, 7=1⋅5+2, 5=2⋅2+1. Back-substituting: 1=5−2⋅2=5−2(7−5)=3⋅5−2⋅7=3(40−5⋅7)−2⋅7=3⋅40−17⋅7. This is the Bézout equation 7x+40y=1 with x=−17.
Reduce.7−1≡−17≡23(mod40). Check: 7⋅23=161=4⋅40+1. Add 40 to land in the range 0 to 39.
Multiply both sides by the inverse.x≡23⋅4=92≡12(mod40). Multiplying both sides of a congruence by the same number keeps it true, and 23⋅7≡1 cancels the coefficient.
Check.7⋅12=84=2⋅40+4. The answer must reproduce the right side.
The second question.gcd(4,6)=2=1, so 4 has no inverse mod 6. Directly, 4x is always even and 1+6k is always odd, so 4x≡1 is impossible. A multiple of 4 and a number of the form 6k+1 can never be equal.
Answer.x≡12(mod40) (every x=12+40t works). The equation 4x≡1(mod6) has no solution.
Corollary B
Division in Z_p
If p is prime and p∤a, then a has an inverse mod p.
If gcd(c,n)=1 and ca≡cb(modn), then a≡b(modn).
In one lineIf p is prime, every integer not divisible by p has an inverse mod p; and if gcd(c,n)=1, you may cancel c: ca≡cb implies a≡b (mod n).
Why it is trueA prime shares no factor with anything it does not divide, so Bézout applies; and cancelling is multiplying by the inverse.
Proofstandard
The idea: Part 1 is the theorem, since gcd(a, p) divides p so is 1 or p. Part 2 multiplies by the inverse of c.
(1) gcd(a,p)=1, so a has an inverse by the theorem.
gcd(a,p) is a positive divisor of p, so it is 1 or p. It is not p because p∤a.
Then a≡(c′c)a=c′(ca)≡c′(cb)=(c′c)b≡b.
(2) Since gcd(c,n)=1, c has an inverse c′ with c′c≡1. Multiply ca≡cb by c′.
So Zp for prime p is a number system where every non-zero element can be divided by, like Q or R but finite. It is the first finite field, which chapter 2.1 uses as the scalars of a vector space.
Fermat’s little theorem
Theorem BFermat 1640; Euler
Fermat's little theorem
Let p be a prime. If p∤a then ap−1≡1(modp). For every integer a (divisible by p or not), ap≡a(modp).
In one lineIf p is prime and p∤a, then ap−1≡1(modp); equivalently ap≡a(modp) for every integer a.
Why it is trueMultiplying all the non-zero classes by a only shuffles them, so the product of all of them is unchanged, which forces ap−1 to be 1.
For p=7 and a=3 the products 3⋅1,3⋅2,…,3⋅6 are 3,6,9,12,15,18, which mod 7 are 3,6,2,5,1,4: the numbers 1 to 6 again, in a different order (see the row of 3 in the table above). That is the whole idea.
ProofHammack 11.4
The idea: Show that a, 2a, ..., (p-1)a are the same p - 1 non-zero classes as 1, 2, ..., p-1 in a different order. Multiply them all together and cancel (p-1)!.
None of a,2a,…,(p−1)a is ≡0(modp).
By Euclid's lemma: p∤a and p∤i for 1≤i≤p−1, so p∤ia.
The classes of a,2a,…,(p−1)a are all different.
Suppose ia≡ja with 1≤i,j≤p−1. Then p∣(i−j)a. Euclid's lemma gives p∣i−j (since p∤a).
So i=j.
∣i−j∣<p−1<p, so p∣i−j forces i=j.
Hence a,2a,…,(p−1)a are congruent to 1,2,…,p−1 in some order.
There are p−1 distinct non-zero classes, and there are exactly p−1 non-zero classes mod p (previous theorem). So we get all of them.
ap−1⋅(p−1)!≡(p−1)!(modp).
Multiply all the congruences together (arithmetic respects congruence); the order of multiplication does not matter.
So ap−1≡1(modp).
(p−1)! is a product of numbers not divisible by p, so by Euclid's lemma p∤(p−1)!, thus gcd((p−1)!,p)=1 and we may cancel it (the corollary).
Therefore ap≡a(modp) for all a.
Multiply by a. If p∣a, both sides of ap≡a are ≡0.
Quick checkWhat is 7100mod11?
11 is prime and 11∤7, so 710≡1(mod11) by Fermat. Then 7100=(710)10≡1. A common mistake is to reduce the exponent mod 11 instead of mod 10.
Your answerWhat is 3100mod7 (a number from 0 to 6)?
Reduce the exponent mod p−1=6.
7 is prime and 7∤3, so 36≡1(mod7). Then 100=16⋅6+4, so 3100≡34=81=11⋅7+4≡4.
Worked example
Compute 5117mod11, and find 3−1mod7 using Fermat.
Solution.
Use Fermat.11 is prime and 11∤5, so 510≡1(mod11). The exponent p−1=10 is the period of the powers.
Reduce the exponent mod 10.117=11⋅10+7, so 5117=(510)11⋅57≡57. Replace 510 by 1.
Compute 57 by squaring.52=25≡3, 54≡32=9, so 57=54⋅52⋅5≡9⋅3⋅5=135=12⋅11+3. 7=4+2+1, and each power is reduced mod 11 as we go.
The inverse.36≡1(mod7), so 3⋅35≡1 and 3−1≡35=243=34⋅7+5≡5. In general a−1≡ap−2(modp).
Answer.5117≡3(mod11), and 3−1≡5(mod7) (check: 3⋅5=15≡1).
Watch out· Fermat's theorem needs p prime and p not dividing a
The converse is false. The number 341=11⋅31 is composite, yet 2340≡1(mod341). Such numbers are why primality tests use more than Fermat. Also, an exponent may be reduced mod p−1 only when p does not divide the base. The base itself is reduced mod p.
Why RSA works
RSA needs one more step: Fermat’s theorem for a product of two primes.
Theorem B
RSA decryption works
Let p=q be primes, n=pq, and e,d positive integers with ed=1+k(p−1)(q−1) for some integer k≥0. Then med≡m(modn) for every integer m.
In one lineIf n=pq for distinct primes and ed≡1(mod(p−1)(q−1)), then (me)d≡m(modn) for every integer m.
Why it is trueFermat's theorem shows med≡m separately mod p and mod q, and two distinct primes dividing a number means their product does.
ProofRivest, Shamir, Adleman 1978
The idea: Show p divides m^(ed) - m and q divides it too, using Fermat; then pq divides it by Euclid's lemma.
Mod p, first suppose p∣m. Then both med and m are ≡0.
Case p∣m: then p divides med and m, so p∣med−m.
If p∤m, then med=m⋅(mp−1)k(q−1)≡m(modp).
Case p∤m: Fermat gives mp−1≡1. Then med=m⋅(mp−1)k(q−1)≡m⋅1=m.
Likewise med≡m(modq).
The identical argument with q in place of p, using ed=1+k(q−1)(p−1).
So p∣med−m and q∣med−m, and hence pq∣med−m.
Write med−m=pt. Then q∣pt. The prime q does not divide the different prime p, so Euclid's lemma gives q∣t.
So med≡m(modn).
That is the definition of congruence mod n.
Where this came from· Gauss, Diffie and Hellman, and RSA
Gauss’s Disquisitiones Arithmeticae (1801) gave the notation a≡b(modn), treated computing with remainders systematically, and stated and proved explicitly that factorization into primes is unique. For two centuries this was pure mathematics. In 1976 Whitfield Diffie and Martin Hellman asked whether two people could agree on a secret over a public channel, and in 1977 Ron Rivest, Adi Shamir and Leonard Adleman at MIT found the system now called RSA (published in 1978), which turns the question into the gap between multiplying primes, which is fast, and factoring their product, which is not known to be. Clifford Cocks at the British agency GCHQ had found an equivalent system in 1973; it stayed secret until 1997.
Examples and non-examples
The worked examples above cover each skill in the problem list: dividing with remainder, Euclid’s algorithm, the extended algorithm, linear equations in integers, unique factorization, computing mod n, inverses, Fermat, and RSA. Three more short ones fix the ideas: zero divisors, check digits, and a proof that uses unique factorization.
Worked example
Is [3]+[4]=[1] in Z6? Find all pairs (a,b) of non-zero classes in Z6 with ab=0.
Solution.
The sum.3+4=7=6+1, so [3]+[4]=[1] in Z6. Reduce the sum mod 6.
Hunt for zero products. A product ab≡0(mod6) means 6∣ab. Try a=2: 2⋅3=6, so [2][3]=0. Try a=4: 4⋅3=12, so [4][3]=0. Also [2][3]=[3][2]=0 and [3][4]=0. Look for pairs whose prime factors 2 and 3 are split between them.
Sanity check with the table. In the mod 6 table, the zeros are at (2,3),(3,2),(3,4),(4,3). The table agrees.
Answer. Yes. The non-zero pairs with product zero are {2,3} and {3,4} (in either order). These “zero divisors” exist exactly because 6 is not prime, which is why Z6 cannot divide.
Worked example
The first twelve digits of an ISBN-13 are 978140289462. Find the check digit, and say whether 9780131103627 is a valid ISBN-13.
The rule: multiply the digits alternately by 1 and 3, starting with 1, and add. A valid ISBN-13 has total ≡0(mod10). The last digit is chosen to make that true.
Solution.
Weight the first twelve digits.9⋅1+7⋅3+8⋅1+1⋅3+4⋅1+0⋅3+2⋅1+8⋅3+9⋅1+4⋅3+6⋅1+2⋅3. The weights run 1,3,1,3,… across the twelve digits, and the thirteenth digit has weight 1.
Choose the check digit c. We need 104+c≡0(mod10), and 104≡4, so c≡−4≡6. The check digit is the number from 0 to 9 that completes the congruence.
Test the second number. Its first twelve digits 978013110362 give 9+21+8+0+1+9+1+3+0+9+6+6=73, and adding the last digit 7 with weight 1 gives 80≡0. The full total, including the check digit, must be a multiple of 10.
Answer. The check digit is 6, and 9780131103627 is valid. Changing any single digit changes the total by w⋅e with w∈{1,3}, which is never ≡0(mod10) because 1 and 3 are invertible mod 10, so every single-digit typing error is caught.
Worked example Euclid IX.20
Prove that there are infinitely many primes.
Solution. The plan: assume there are only finitely many, build a number that no prime on the list can divide, and use the fundamental theorem to find a contradiction.
Assume the opposite. Suppose the primes are exactly p1,p2,…,pk. Proof by contradiction (0.2): we assume the negation of the claim.
Build a number. Let N=p1p2⋯pk+1. Then N>1. Every pi divides the product, so dividing N by pi always leaves remainder 1.
Find a prime divisor. By the fundamental theorem N is a product of primes, so some prime p divides N. Since the list contains all primes, p=pi for some i. Existence of a prime factorization, applied to N.
Get the contradiction. Then pi∣N and pi∣p1⋯pk, so pi∣N−p1⋯pk=1. But no prime divides 1. Fact 1: a divisor of two numbers divides their difference. A prime is greater than 1, and a divisor of 1 has size at most 1 (fact 3).
Answer. The assumption is impossible, so there are infinitely many primes. The proof does not claim that N itself is prime.
Worked example
Solve the system x≡3(mod4) and x≡2(mod7).
Solution.
Check the moduli are coprime.gcd(4,7)=1. Then Bézout gives 4u+7v=1, which is the tool for gluing the two conditions.
Find u,v.4⋅2+7⋅(−1)=1, so u=2, v=−1. Extended Euclid, or just trial for numbers this small.
Build a solution. Take x0=3⋅7⋅(−1)+2⋅4⋅2=−21+16=−5. The term 7v≡1(mod4) and 4u≡1(mod7). So 7v acts like 1 mod 4 and like 0 mod 7, and 4u acts like 0 mod 4 and like 1 mod 7. The combination 3⋅(7v)+2⋅(4u) is then 3 mod 4 and 2 mod 7.
Reduce.−5+28=23. Adding the product of the moduli, 28, changes neither congruence.
Check.23=5⋅4+3 and 23=3⋅7+2. The answer must reproduce both remainders.
Answer.x≡23(mod28), and every solution has this form.
Where it is used
Used inside maths· Inside maths: fields, groups, and the Euclidean algorithm for polynomials
Zp for a prime p is a finite field, so you can do linear algebra over it: vector spaces, bases, and determinants all work with scalars from Zp (chapter 2.1 uses fields). The invertible classes mod n form a group under multiplication, with φ(n) elements (the number of k from 1 to n with gcd(k,n)=1). Lagrange’s theorem (MATH 541) then gives Euler’s generalisation of Fermat: aφ(n)≡1(modn) when gcd(a,n)=1. The same Euclid’s-algorithm-and-Bézout argument works for polynomials, where the remainder has smaller degree, and gives the gcd of two polynomials: this is the generalisation that carries Bézout’s name.
Used in the real world· Real systems: RSA encryption, check digits, and hash tables
RSA with real numbers (tiny). Take the primes p=5 and q=11. Then n=pq=55 and (p−1)(q−1)=4⋅10=40. Choose e=3, which is coprime to 40. The private exponent d is the inverse of 3 mod 40. Publish (n,e)=(55,3) and keep d secret.
Your answerTake the RSA primes p=7 and q=11 with public exponent e=7. What is the private exponent d, as the smallest positive number with ed≡1(mod(p−1)(q−1))?
First compute (p−1)(q−1), then find the inverse of 7 modulo that number with the extended Euclid.
(p−1)(q−1)=6⋅10=60. We need 7d≡1(mod60). Euclid: 60=8⋅7+4, 7=1⋅4+3, 4=1⋅3+1, so 1=4−3=4−(7−4)=2⋅4−7=2(60−8⋅7)−7=2⋅60−17⋅7. So d≡−17≡43. Check: 7⋅43=301=5⋅60+1.
Real RSA uses primes of about 1024 bits each, so n has about 617 digits, and a common public exponent is e=65537. Here anyone can factor 55=5⋅11 at once, compute 40, and invert 3. With a 617-digit n nobody knows how to factor it in reasonable time, while Euclid’s algorithm and fast squaring keep encryption and decryption quick. (Real systems also pad messages; raw RSA as shown is for understanding only.)
Check digits. The last digit of a code is chosen to make a congruence hold, so typing errors break it.
ISBN-13 (books): multiply the digits alternately by 1 and 3 and add; the total must be ≡0(mod10). For 978-0-306-40615-7 the sum is 9+21+8+0+3+0+6+12+0+18+1+15+7=100. A single wrong digit changes the sum by w⋅e with weight w∈{1,3} and error 0<∣e∣≤9. Both weights are coprime to 10, so they are invertible, and we≡0 would force e≡0(mod10), which is impossible. So every single-digit error is caught. Swapping two adjacent digits x,y changes the sum by 2(x−y), which is ≡0 only when x−y=±5, so swaps like 0↔5 slip through.
ISBN-10: weights 10,9,…,1 and modulus 11. For 0-306-40615-2 the sum is 132=11⋅12. Because 11 is prime, every weight is invertible and the difference of two different digits is never a multiple of 11, so every single-digit error and every swap of adjacent different digits is caught. (The check digit may be 10, written X.)
UPC-A (barcodes): weights 3,1,3,1,… on the 12 digits, total ≡0(mod10). For 036000291452: 3(0+6+0+2+1+5)+(3+0+0+9+4+2)=42+18=60.
Luhn (credit cards, devised by Hans Peter Luhn of IBM in the 1950s): from the right, double every second digit (subtracting 9 if the result exceeds 9), add everything, and require a multiple of 10. For 79927398713 the processed digits are 7,9,9,4,7,6,9,7,7,2,3 with sum 70. The doubling map sends the digits 0,1,…,9 to 0,2,4,6,8,1,3,5,7,9, a shuffle of the digits, so changing one digit always changes the sum mod 10. It catches every adjacent swap except 09↔90. (Aadhaar numbers use a different check, the Verhoeff scheme, built from the symmetries of a pentagon, which catches all adjacent swaps; its algebra is a group, seen in MATH 541.)
Hash tables. A hash function h(k)=kmodm puts key k in one of m buckets. Take the ten keys 100,110,…,190. With m=10 every key lands in bucket 0, since each is a multiple of 10. With m=11 they land in buckets 1,0,10,9,8,7,6,5,4,3, all different, because 10 is coprime to 11. In general, if every key is a multiple of g and g divides m, only m/g of the buckets are ever used, which is one reason table sizes are often chosen prime. And by the pigeonhole principle of 0.3 no hash into m buckets can avoid collisions once there are more than m keys.
Traps
Watch out· Remainders are never negative
−7mod3=2, not −1. Some programming languages (C, Java) return −1 for the remainder of −7/3 and others (Python) return 2. In mathematics the remainder is in 0,…,n−1. The two are congruent mod 3, so either is fine inside a congruence, but they are different answers to “what is the remainder”.
Watch out· 'mod' is used for two different things
amodn is a number, the remainder of a. a≡b(modn) is a statement about a and b. We have 17≡2(mod5) and 17mod5=2, but 17mod5=2 is an equation between numbers while the first line is a relation.
Watch out· Cancelling needs a unit
You may cancel c from ca≡cb(modn) only when gcd(c,n)=1. In general you can only cancel to a≡b(modn/gcd(c,n)). For example 2⋅1≡2⋅4(mod6) gives 1≡4(mod3), which is true, not 1≡4(mod6).
Watch out· Reducing exponents
Exponents reduce mod p−1 (when p∤a), not mod p: ap−1≡1, so ak depends on kmod(p−1). For p=7: 38≡32=9≡2, not 38mod7=31.
Watch out· The number 1 is not prime
The integer 1 is not prime, and negative numbers are not called prime in this book. A prime has exactly two positive divisors, which is why 1 (one divisor) is excluded. Without this, unique factorization would fail (6=2⋅3=1⋅2⋅3).
Problems
check
Find the quotient and remainder when 1000 is divided by 7, and when −45 is divided by 8.
Solution
1000=7⋅142+6, so q=142, r=6. −45=8⋅(−6)+3, so q=−6, r=3 (check: −48+3=−45).
check
Compute gcd(126,84) by Euclid’s algorithm, and gcd(91,35).
Solution
126=1⋅84+42, 84=2⋅42+0, so gcd(126,84)=42. 91=2⋅35+21, 35=1⋅21+14, 21=1⋅14+7, 14=2⋅7, so gcd(91,35)=7.
check
Does 2x≡3(mod6) have a solution? Does 5x≡1(mod12)?
Solution
gcd(2,6)=2∤3, so the first has no solution (2x−3 is odd, never a multiple of 6). gcd(5,12)=1, and 5⋅5=25=2⋅12+1, so x≡5 is a solution.
checkCS 240
Find 350mod5 and the last digit of 7100.
Solution
34=81≡1(mod5), 50=4⋅12+2, so 350≡32=9≡4. For the last digit take mod 10: 74=2401≡1(mod10) and 100=4⋅25, so 7100≡1. The last digit is 1.
use
Find integers x,y with 91x+35y=7.
Solution
From the Euclid lines 91=2⋅35+21, 35=1⋅21+14, 21=1⋅14+7: 7=21−14=21−(35−21)=2⋅21−35=2(91−2⋅35)−35=2⋅91−5⋅35. So x=2, y=−5. Check: 182−175=7.
use
Solve 17x≡5(mod43).
Solution
gcd(17,43)=1. Euclid: 43=2⋅17+9, 17=1⋅9+8, 9=1⋅8+1. Back: 1=9−8=9−(17−9)=2⋅9−17=2(43−2⋅17)−17=2⋅43−5⋅17. So 17−1≡−5≡38. Then x≡5⋅38=190=4⋅43+18≡18. Check: 17⋅18=306=7⋅43+5.
use
Use Fermat’s theorem to find 2100mod13.
Solution
13 is prime and 13∤2, so 212≡1(mod13). 100=8⋅12+4, so 2100≡24=16≡3.
use
RSA with p=7, q=13, e=5. Find n, the private exponent d, and encrypt m=10. Check that decryption returns 10.
Solution
n=91 and (p−1)(q−1)=6⋅12=72. gcd(5,72)=1. Find d: 72=14⋅5+2, 5=2⋅2+1, so 1=5−2⋅2=5−2(72−14⋅5)=29⋅5−2⋅72, so d=29 (5⋅29=145=2⋅72+1). Encrypt: 105=100000=1098⋅91+82, so c=82. Decrypt: 8229mod91=10 (check by repeated squaring, or by Fermat mod 7 and mod 13 separately, as in the proof).
use
Is the ISBN-13 9780306406158 valid? What should its last digit be?
Solution
Weights 1,3,1,3,… on the first twelve digits 9,7,8,0,3,0,6,4,0,6,1,5 give 9+21+8+0+3+0+6+12+0+18+1+15=93. The check digit must make the total a multiple of 10, so it is 7. The last digit 8 gives 101, which is not, so the number is invalid.
proveHammack 5.4
Prove that gcd(a,b)=gcd(a,b+ka) for every integer k (with a and b not both zero).
Solution
Let d divide a and b. Then d divides b+ka by fact 1, so every common divisor of (a,b) is a common divisor of (a,b+ka). Conversely if d divides a and b+ka then d divides (b+ka)−ka=b. So the two pairs have the same common divisors and the same greatest one.
prove
Prove: if gcd(a,n)=1 and gcd(b,n)=1 then gcd(ab,n)=1.
Solution
By Bézout, ax+ny=1 and bu+nv=1 for integers x,y,u,v. Multiply: 1=(ax+ny)(bu+nv)=ab(xu)+n(axv+ybu+nyv). This is a combination of ab and n equal to 1, so any common divisor of ab and n divides 1, and gcd(ab,n)=1.
prove
Prove: if a∣c, b∣c and gcd(a,b)=1, then ab∣c. Is it true without the coprime condition?
Solution
Write c=ak and c=bl, and take ax+by=1 (Bézout). Then c=cax+cby=(bl)ax+(ak)by=ab(lx+ky), so ab∣c. Without coprimality it fails: 6∣12 and 4∣12 but 24∤12.
proveEuclid IX.20
Prove that for every positive integer n there is a prime p>n. (Look at n!+1.)
Solution
The number N=n!+1 is greater than 1, so it has a prime divisor p by the fundamental theorem. Suppose p≤n. Then p is one of the factors 1,2,…,n of n!, so p∣n!. Also p∣N, so p∣N−n!=1 by fact 1, which is impossible for a prime. So p>n. (Hence there is no largest prime.)
prove
Prove: gcd(a,b)⋅lcm(a,b)=ab for positive integers, using the prime factorizations of a and b.
Solution
Write a=∏pisi and b=∏piti over the same list of primes, allowing exponents 0. By unique factorization the gcd is ∏pimin(si,ti) and the lcm is ∏pimax(si,ti) (a common divisor can use each prime at most as often as it appears in both, a common multiple at least as often as in either). For each i, min(si,ti)+max(si,ti)=si+ti, so the product of gcd and lcm has exponent si+ti on pi, which is the exponent of ab. Example: 12⋅2520=30240=360⋅84.
stretch
Prove gcd(2m−1,2n−1)=2gcd(m,n)−1, and check it for m=12, n=18.
Solution
Let m=nq+r with 0≤r<n. Then 2m−1=2r(2nq−1)+(2r−1). Since x−1 divides xq−1 (geometric sum), 2n−1 divides 2nq−1, so 2m−1=(2n−1)t+(2r−1) for an integer t. By Euclid’s reduction step, gcd(2m−1,2n−1)=gcd(2n−1,2r−1). This is Euclid’s algorithm run on the exponents m,n,r,…, so it ends with gcd(2g−1,20−1)=gcd(2g−1,0)=2g−1, where g=gcd(m,n) is the last non-zero exponent. For m=12, n=18: g=6 and 26−1=63. Direct: 212−1=4095=63⋅65 and 218−1=262143=63⋅4161, with gcd(65,4161)=1.
stretchChinese remainder theorem
Let gcd(m,n)=1. Prove that for any integers a,b the system x≡a(modm), x≡b(modn) has a solution, and that it is unique mod mn. Solve x≡2(mod3), x≡3(mod5).
Solution
By Bézout, mu+nv=1 for some integers u,v. Put x0=anv+bmu. Mod m: nv=1−mu≡1, and bmu≡0, so x0≡a. Mod n: mu=1−nv≡1 and anv≡0, so x0≡b. Uniqueness: if x and x′ are both solutions then m∣x−x′ and n∣x−x′, so mn∣x−x′ by the coprime problem above. For m=3, n=5: 3⋅2+5⋅(−1)=1, so u=2, v=−1 and x0=2⋅5⋅(−1)+3⋅3⋅2=−10+18=8. Check: 8=3⋅2+2=5+3. All solutions are x≡8(mod15).
Summary
Division algorithm: for b>0, a=bq+r with 0≤r<b, q and r unique (proved by well-ordering). Remainders are never negative.
Euclid’s algorithm replaces (a,b) by (b,r) without changing the gcd, and ends at the last non-zero remainder.
Bézout:gcd(a,b)=ax+by for some integers x,y (the extended Euclid finds them). Hence ax+by=c is solvable iff gcd(a,b)∣c.
Euclid’s lemma: a prime dividing a product divides a factor. It gives unique factorization into primes.
a≡b(modn) means n∣a−b. It is an equivalence relation with exactly n classes, and + and × on classes are well defined, as for fractions in 0.4.
Inverses:a has an inverse mod n iff gcd(a,n)=1; so you can divide mod a prime, and cancel a factor only if it is coprime to the modulus.
Fermat:ap−1≡1(modp) for p∤a. It makes RSA work: encryption is me, decryption cd, with ed≡1(mod(p−1)(q−1)), and the security is the difficulty of factoring n=pq.
Check digits (ISBN, UPC, Luhn) and hash tables are congruences in disguise; their strengths and weaknesses come from which weights and moduli are invertible.
Watch and read
The same ideas explained by someone else. Use these when an explanation here does not land.
CS 240: CS 240 gave you the definitions of divides, prime, gcd and mod, and used them in proofs about even and odd. This chapter proves the theorems behind them: the division algorithm, Bézout, unique factorization. The Properties and Definitions sheet becomes provable.
MATH 541: $\mathbb Z_n$ is the first example of a ring, and $\mathbb Z_p$ for prime $p$ is the first finite field. The set of invertible classes mod $n$ is a group, and Fermat's little theorem is Lagrange's theorem for that group.
MATH 431: Pseudo-random number generators are recurrences mod $n$, and their quality depends on gcd conditions between the multiplier and the modulus.
Cryptography and security: RSA, Diffie-Hellman and elliptic-curve cryptography all compute in $\mathbb Z_n$ or $\mathbb Z_p$. Nothing beyond this chapter is needed to understand how RSA decrypts correctly.