Survey of Mathematics

0.5 Integers mod n, gcd and Bézout

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 100100 days from today? You divide 100100 by 77 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≡43x \equiv 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?
Quick checkFor the relation a∼ba \sim b iff 5∣(a−b)5 \mid (a - b), which of 33, 1313, −2-2 are in the class [8][8]?
Quick checkWhat is the remainder when −7-7 is divided by 33?

The picture

Take a week of seven days, numbered 00 (Sunday) to 66 (Saturday). Counting 100100 days forward from a Wednesday (33) moves you 100 mod 7=2100 \bmod 7 = 2 places ahead, since 100=14⋅7+2100 = 14 \cdot 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 77 and mod 66.

×\times mod 7123456
1123456
2246135
3362514
4415263
5531642
6654321
×\times mod 612345
112345
224024
330303
442042
554321

Look at the row of 33 in the first table: it contains 11 exactly once (3⋅5≡13 \cdot 5 \equiv 1), so 33 has an “inverse” 55 that undoes multiplication by 33. Every row of the mod 7 table is a shuffle of 1,…,61, \dots, 6. In the mod 6 table the rows of 22, 33 and 44 have repeats and no 11: those numbers have no inverse, and 2⋅3≡02 \cdot 3 \equiv 0 even though neither factor is 00. The difference between 77 and 66 is that 77 is prime. The exact rule, for any modulus, is the main theorem of this chapter: aa has an inverse mod nn iff gcd⁡(a,n)=1\gcd(a, n) = 1.

The idea, in words

Why· The chain of ideas

Everything follows from one picture. Dividing aa by bb with a remainder splits aa into a multiple of bb plus a leftover smaller than bb. If a number dd divides both aa and bb, it also divides the leftover, so the common divisors of (a,b)(a, b) are the common divisors of (b,leftover)(b, \text{leftover}), and the problem has become smaller. Repeating gives Euclid’s algorithm. Running the algorithm backwards writes the gcd as ax+byax + by, which is Bézout’s identity. That identity is a way to divide: if gcd⁡(a,n)=1\gcd(a, n) = 1 then ax+ny=1ax + ny = 1, so ax≡1(modn)ax \equiv 1 \pmod n and xx is an inverse of aa. 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+byax + 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 B Hammack 5.2

Division algorithm

Let aa be an integer and bb a positive integer. Then there exist unique integers qq (the quotient) and rr (the remainder) such that

a=bq+r,0≤r<b.a = bq + r, \qquad 0 \le r < b.

We write r=a mod br = a \bmod b.

In one lineFor any integer aa and positive integer bb there are unique integers qq and rr with a=bq+ra = bq + r and 0≤r<b0 \le r < b.

Why it is trueSubtract multiples of bb from aa 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}S = \{a - bk : k \in \mathbb Z,\ a - bk \ge 0\}.
We consider every leftover a−bka - bk we can get by subtracting multiples of bb and that is not negative.
SS is non-empty.
Take k=−∣a∣k = -\lvert a \rvert: then a−bk=a+b∣a∣≥a+∣a∣≥0a - bk = a + b\lvert a \rvert \ge a + \lvert a \rvert \ge 0, since b≥1b \ge 1.
So SS has a least element; call it r=a−bqr = a - bq.
If 0∈S0 \in S then 00 is the smallest possible element. Otherwise every element of SS is a positive integer, and the well-ordering principle (0.2) applies.
Then r≥0r \ge 0 and a=bq+ra = bq + r.
By the definition of SS, r≥0r \ge 0.
Suppose r≥br \ge b.
Proof by contradiction. If r≥br \ge b then r−b=a−b(q+1)≥0r - b = a - b(q + 1) \ge 0, so r−b∈Sr - b \in S, and it is smaller than rr. That contradicts rr being the least element.
Hence r<br < b. This proves existence.
The assumption led to a contradiction.
So bb divides r′−rr' - r.
Uniqueness: assume a=bq+r=bq′+r′a = bq + r = bq' + r' with 0≤r≤r′<b0 \le r \le r' < b (swap the names if needed). Subtract: b(q−q′)=r′−rb(q - q') = r' - r.
So r′=rr' = r, and then b(q−q′)=0b(q - q') = 0 gives q=q′q = q'.
But 0≤r′−r<b0 \le r' - r < b, and the only multiple of bb in that range is 00.
Worked example
Find the quotient and remainder when (a) 100100 is divided by 77 and (b) −17-17 is divided by 55.

Solution.

  1. (a) Find the largest multiple not above 100. 7⋅14=98≤100<105=7⋅157 \cdot 14 = 98 \le 100 < 105 = 7 \cdot 15. So q=14q = 14. The quotient is the number of full sevens that fit.
  2. (a) The leftover. r=100−98=2r = 100 - 98 = 2. The remainder is a−bqa - bq, and it lies between 00 and 66.
  3. (b) Negative number: go below, not above. 5⋅(−4)=−20≤−17<−15=5⋅(−3)5 \cdot (-4) = -20 \le -17 < -15 = 5 \cdot (-3). So q=−4q = -4. The remainder must be non-negative, so the multiple of 55 must not exceed −17-17. Choosing q=−3q = -3 would give r=−17+15=−2r = -17 + 15 = -2, which is negative.
  4. (b) The leftover. r=−17−(−20)=3r = -17 - (-20) = 3. −17=5⋅(−4)+3-17 = 5 \cdot (-4) + 3.

Answer. (a) 100=7⋅14+2100 = 7 \cdot 14 + 2. (b) −17=5⋅(−4)+3-17 = 5 \cdot (-4) + 3, so −17 mod 5=3-17 \bmod 5 = 3.

Divisibility

Definition B

Divides

For integers aa and bb, aa divides bb, written a∣ba \mid b, if there is an integer kk with b=akb = ak. We also say aa is a divisor or factor of bb and bb is a multiple of aa. When aa does not divide bb, we write a∤ba \nmid b. Note a∣0a \mid 0 for every aa (take k=0k = 0), and 0∣b0 \mid b only if b=0b = 0.

In one linea∣ba \mid b means bb is a whole-number multiple of aa: b=akb = ak for some integer kk.

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,ca, b, c:

  1. If a∣ba \mid b and a∣ca \mid c, then a∣(bx+cy)a \mid (bx + cy) for all integers x,yx, y.
  2. If a∣ba \mid b and b∣cb \mid c, then a∣ca \mid c.
  3. If a∣ba \mid b and b≠0b \ne 0, then ∣a∣≤∣b∣\lvert a \rvert \le \lvert b \rvert.

In one lineDivisors pass through sums and multiples: if aa divides bb and cc, it divides bx+cybx + cy for all integers x,yx, 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=akb = ak.

Proofstandard
The idea: Write each hypothesis as an equation b = ak and substitute.
(1) Write b=akb = ak and c=alc = al.
Definition of divides, used for both hypotheses.
Then bx+cy=a(kx+ly)bx + cy = a(kx + ly), which is a multiple of aa.
Substitute and factor out aa. The bracket is an integer because sums and products of integers are integers.
Then c=(ak)l=a(kl)c = (ak)l = a(kl), a multiple of aa.
(2) Write b=akb = ak and c=blc = bl. Substitute.
Then ∣b∣=∣a∣∣k∣≥∣a∣\lvert b \rvert = \lvert a \rvert \lvert k \rvert \ge \lvert a \rvert.
(3) Write b=akb = ak. Since b≠0b \ne 0, k≠0k \ne 0, and a non-zero integer has size at least 11.

The greatest common divisor

Definition B

Gcd

For integers a,ba, b not both zero, gcd⁡(a,b)\gcd(a, b) is the largest positive integer that divides both aa and bb. It exists: 11 is a common divisor, and by fact 3 every common divisor is at most max⁡(∣a∣,∣b∣)\max(\lvert a \rvert, \lvert b \rvert) in size, so there are only finitely many. We say aa and bb are coprime (or relatively prime) if gcd⁡(a,b)=1\gcd(a, b) = 1. For example gcd⁡(12,18)=6\gcd(12, 18) = 6, gcd⁡(9,16)=1\gcd(9, 16) = 1, and gcd⁡(a,0)=∣a∣\gcd(a, 0) = \lvert a \rvert.

In one lineThe greatest common divisor of aa and bb 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,ra, b, q, r are integers with a=bq+ra = bq + r and b≠0b \ne 0, then gcd⁡(a,b)=gcd⁡(b,r)\gcd(a, b) = \gcd(b, r) (where gcd⁡(b,0)=∣b∣\gcd(b, 0) = \lvert b \rvert).

In one lineIf a=bq+ra = bq + r then aa and bb have exactly the same common divisors as bb and rr, so gcd⁡(a,b)=gcd⁡(b,r)\gcd(a, b) = \gcd(b, r).

Why it is trueA number dividing aa and bb divides a−bq=ra - bq = r, and a number dividing bb and rr divides bq+r=abq + 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 dd divide both aa and bb. Then d∣a−bq=rd \mid a - bq = r, so dd is a common divisor of bb and rr.
Fact 1 with x=1x = 1 and y=−qy = -q: a divisor of aa and bb divides a−qba - qb.
Conversely let dd divide both bb and rr. Then d∣bq+r=ad \mid bq + r = a, so dd is a common divisor of aa and bb.
Fact 1 again: a divisor of bb and rr divides qb+rqb + r.
The pairs (a,b)(a, b) and (b,r)(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,ba, b, define r0=ar_0 = a, r1=br_1 = b, and, as long as ri≠0r_i \ne 0, divide: ri−1=qiri+ri+1r_{i-1} = q_i r_i + r_{i+1} with 0≤ri+1<ri0 \le r_{i+1} < r_i. Then the sequence reaches 00, and the last non-zero term rnr_n equals gcd⁡(a,b)\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>⋯≥0r_1 > r_2 > \dots \ge 0 must reach 00 after finitely many steps; say rn+1=0r_{n+1} = 0.
By the division algorithm each remainder satisfies 0≤ri+1<ri0 \le r_{i+1} < r_i, so r1>r2>r3>…r_1 > r_2 > r_3 > \dots. A strictly decreasing sequence of non-negative integers has only finitely many terms (by well-ordering: its set of values has a least element).
gcd⁡(a,b)=gcd⁡(r1,r2)=gcd⁡(r2,r3)=⋯=gcd⁡(rn,rn+1)\gcd(a, b) = \gcd(r_1, r_2) = \gcd(r_2, r_3) = \dots = \gcd(r_n, r_{n+1}).
Euclid's reduction step applied to each line ri−1=qiri+ri+1r_{i-1} = q_i r_i + r_{i+1}.
So gcd⁡(a,b)=rn\gcd(a, b) = r_n, the last non-zero remainder.
rn+1=0r_{n+1} = 0, and gcd⁡(rn,0)=rn\gcd(r_n, 0) = r_n.
Worked example
Compute gcd⁡(252,105)\gcd(252, 105) with Euclid's algorithm.

Solution.

  1. Divide the larger by the smaller. 252=2⋅105+42252 = 2 \cdot 105 + 42. 2⋅105=210≤252<3152 \cdot 105 = 210 \le 252 < 315, leaving 252−210=42252 - 210 = 42.
  2. Replace the pair. Now work with (105,42)(105, 42): 105=2⋅42+21105 = 2 \cdot 42 + 21. The reduction step: gcd⁡(252,105)=gcd⁡(105,42)\gcd(252, 105) = \gcd(105, 42).
  3. Again. 42=2⋅21+042 = 2 \cdot 21 + 0. Remainder zero, so we stop. gcd⁡(105,42)=gcd⁡(42,21)=21\gcd(105, 42) = \gcd(42, 21) = 21.

Answer. gcd⁡(252,105)=21\gcd(252, 105) = 21. Check: 252=12⋅21252 = 12 \cdot 21 and 105=5⋅21105 = 5 \cdot 21, and 1212 and 55 have no common factor.

Your answerUse Euclid's algorithm to find gcd⁡(1547,560)\gcd(1547, 560).

Bézout’s identity

Euclid’s algorithm finds the gcd. Running it backwards finds something more useful.

Theorem B Bachet 1624; Hammack 5.3

Bézout's identity

If a,ba, b are integers, not both zero, there exist integers x,yx, y with ax+by=gcd⁡(a,b)ax + by = \gcd(a, b). Moreover gcd⁡(a,b)\gcd(a, b) is the smallest positive integer that can be written as ax+byax + by.

In one lineThe gcd of aa and bb is always an integer combination of them: gcd⁡(a,b)=ax+by\gcd(a, b) = ax + by for some integers x,yx, y. In fact it is the smallest positive number of that form.

Why it is trueLook at all positive numbers of the form ax+byax + by. The smallest one must divide aa and bb, 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}S = \{ax + by : x, y \in \mathbb Z,\ ax + by > 0\}.
We study all the positive integers that can be built from aa and bb with integer coefficients.
SS is non-empty, so it has a least element d=ax0+by0d = ax_0 + by_0.
Not both of a,ba, b are zero, so a⋅a+b⋅b>0a \cdot a + b \cdot b > 0 lies in SS. Well-ordering (0.2) gives a least element.
Divide aa by dd: a=qd+ra = qd + r with 0≤r<d0 \le r < d.
The division algorithm (above) applied to aa and the positive number dd.
r=a−qd=a(1−qx0)+b(−qy0)r = a - qd = a(1 - qx_0) + b(-qy_0).
Substitute d=ax0+by0d = ax_0 + by_0: the remainder is again a combination of aa and bb.This is the key idea. A remainder from dividing by a combination is itself a combination.
So r=0r = 0, that is d∣ad \mid a. The same argument gives d∣bd \mid b.
If r>0r > 0 it would be a positive combination smaller than dd, contradicting that dd is the least element of SS.
Therefore d=gcd⁡(a,b)d = \gcd(a, b).
Now dd is a common divisor, so d≤gcd⁡(a,b)d \le \gcd(a, b). Conversely gcd⁡(a,b)\gcd(a, b) divides aa and bb, so by fact 1 it divides ax0+by0=dax_0 + by_0 = d, and by fact 3 it is at most dd.
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.

    Quick checkWhich of these integers can be written as 12x+18y12x + 18y with integer x,yx, y?
    Corollary B

    Linear combinations of two integers

    Let g=gcd⁡(a,b)g = \gcd(a, b). The equation ax+by=cax + by = c has an integer solution if and only if g∣cg \mid c.

    In one lineax+by=cax + by = c has an integer solution (x,y)(x, y) exactly when gcd⁡(a,b)\gcd(a, b) divides cc. In particular aa and bb are coprime iff ax+by=1ax + 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=cax + by = c, then g∣cg \mid c.
    Fact 1: gg divides aa and bb, hence divides ax+byax + by.
    Conversely, write c=gkc = gk and take ax0+by0=gax_0 + by_0 = g.
    If g∣cg \mid c then c=gkc = gk. Bézout gives ax0+by0=gax_0 + by_0 = g.
    Then a(kx0)+b(ky0)=gk=ca(kx_0) + b(ky_0) = gk = c, so (kx0,ky0)(kx_0, ky_0) is a solution.
    Multiply the Bézout identity by kk.

    How do you find xx and yy? 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*b
    
    g, 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 % n
    
    print("inverse of 11 mod 26:", inverse(11, 26))

    Try this. Check the trace above with ext_gcd(240, 46). Then find the inverse of 1111 mod 2626 (the answer should be 1919), 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=14240x + 46y = 14 in integers, and decide whether 240x+46y=15240x + 46y = 15 is solvable.

    Solution.

    1. Compute the gcd. From the trace above, gcd⁡(240,46)=2\gcd(240, 46) = 2. By the corollary, solvability depends only on whether the gcd divides the right side.
    2. Test 1414. 2∣142 \mid 14, so a solution exists. The corollary, “if” direction.
    3. Scale the Bézout identity. From 240(−9)+46(47)=2240(-9) + 46(47) = 2, multiply by 77: 240(−63)+46(329)=14240(-63) + 46(329) = 14. 14=7⋅214 = 7 \cdot 2. Check: −15120+15134=14-15120 + 15134 = 14.
    4. More solutions. Replacing (x,y)(x, y) by (x+23t, y−120t)(x + 23t,\ y - 120t) keeps the value, since 240⋅23=5520=46⋅120240 \cdot 23 = 5520 = 46 \cdot 120. Adding 2323 copies of 240240 and subtracting 120120 copies of 4646 changes the total by 5520−5520=05520 - 5520 = 0.
    5. Test 1515. 2∤152 \nmid 15, so there is no solution. Every value 240x+46y240x + 46y is even.

    Answer. (x,y)=(−63,329)(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>1p > 1 is prime if its only positive divisors are 11 and pp. An integer n>1n > 1 that is not prime is composite: n=abn = ab with 1<a,b<n1 < a, b < n. The number 11 is neither. The first primes are 2,3,5,7,11,13,…2, 3, 5, 7, 11, 13, \dots

    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⋅36 \mid 2 \cdot 3 but 66 divides neither 22 nor 33). It needs Bézout.

    Lemma B Euclid VII.30

    Euclid's lemma

    Let pp be prime and a,ba, b integers. If p∣abp \mid ab, then p∣ap \mid a or p∣bp \mid b. More generally, if p∣a1a2⋯akp \mid a_1 a_2 \cdots a_k then p∣aip \mid a_i for some ii.

    In one lineIf a prime pp divides a product abab, then pp divides aa or pp divides bb.

    Why it is trueIf pp does not divide aa, then pp and aa are coprime, so Bézout gives 1=px+ay1 = px + ay, and multiplying by bb shows p∣bp \mid 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∤ap \nmid a. We show p∣bp \mid b.
    Proof by cases (0.2): either p∣ap \mid a, and we are done, or p∤ap \nmid a. We study the second case.
    So gcd⁡(p,a)=1\gcd(p, a) = 1.
    gcd⁡(p,a)\gcd(p, a) is a positive divisor of pp, so it is 11 or pp. It is not pp, since p∤ap \nmid a.
    Choose integers x,yx, y with px+ay=1px + ay = 1.
    Bézout's identity.
    Then pbx+aby=bpbx + aby = b.
    Multiply both sides by bb.
    Both terms on the left are multiples of pp.
    pp divides pbxpbx because pbx=p(bx)pbx = p(bx), and pp divides abab by hypothesis, so pp divides abyaby.
    So p∣bp \mid b.
    Fact 1: a divisor of two numbers divides their sum.
    The version for kk factors follows by induction on kk.
    For kk factors, induct on kk: apply the two-factor case to a1a_1 and the product a2⋯aka_2 \cdots a_k.
    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.

      Worked example Hammack 5.4
      Prove: if a∣bca \mid bc and gcd⁡(a,b)=1\gcd(a, b) = 1, then a∣ca \mid c.

      Solution. The plan: this is Euclid’s lemma with aa not required to be prime. Use Bézout to get a combination equal to 11, then multiply by cc.

      1. Use Bézout. Since gcd⁡(a,b)=1\gcd(a, b) = 1, there are integers x,yx, y with ax+by=1ax + by = 1. The coprime hypothesis is only usable through Bézout.
      2. Multiply by cc. acx+bcy=cacx + bcy = c. We want cc alone on one side, because the goal is to show aa divides cc.
      3. Check both terms. a∣acxa \mid acx because it has the factor aa. a∣bcya \mid bcy because a∣bca \mid bc by hypothesis. The first term is a multiple of aa by its shape, the second by the hypothesis.
      4. Add. So aa divides acx+bcy=cacx + bcy = c. A divisor of two numbers divides their sum (fact 1).

      Answer. a∣ca \mid c. For example, 6∣4⋅96 \mid 4 \cdot 9 does not give 6∣96 \mid 9, because gcd⁡(6,4)=2≠1\gcd(6, 4) = 2 \ne 1.

      Quick check6∣4⋅9=366 \mid 4 \cdot 9 = 36, but 6∤46 \nmid 4 and 6∤96 \nmid 9. Why does this not contradict Euclid's lemma?
      Theorem B Euclid IX.14; Gauss 1801

      Fundamental theorem of arithmetic

      Every integer n>1n > 1 can be written as a product of primes n=p1p2⋯prn = p_1 p_2 \cdots p_r (a prime is a product of one prime). If p1⋯pr=q1⋯qsp_1 \cdots p_r = q_1 \cdots q_s with all pi,qjp_i, q_j prime, then r=sr = s and the qjq_j are the pip_i 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)S(2) holds, since 22 is prime. Let n>2n > 2 and assume S(k)S(k) for 2≤k<n2 \le k < n.
      Existence, by strong induction on nn (0.2). Let S(n)S(n) say that nn is a product of primes. Assume S(k)S(k) for all 2≤k<n2 \le k < n.
      Case 1: nn is prime. Done.
      If nn is prime it is a product of one prime, so S(n)S(n) holds.
      Case 2: n=abn = ab with 2≤a,b<n2 \le a, b < n.
      Case 2: nn is not prime, so n=abn = ab with 1<a,b<n1 < a, b < n (a divisor other than 11 and nn; the other factor b=n/ab = n/a is also between 11 and nn by fact 3).
      Both aa and bb are products of primes, so n=abn = ab is a product of primes.
      The strong induction hypothesis applies to aa and to bb.
      Take the last prime on the left, prp_r. It divides some qjq_j.
      Uniqueness, by induction on rr. Suppose p1⋯pr=q1⋯qsp_1 \cdots p_r = q_1 \cdots q_s. Then prp_r divides q1⋯qsq_1 \cdots q_s, so by Euclid's lemma pr∣qjp_r \mid q_j for some jj.
      So pr=qjp_r = q_j.
      qjq_j is prime, so its only positive divisors are 11 and qjq_j. And pr>1p_r > 1.
      Then p1⋯pr−1=q1⋯qs−1p_1 \cdots p_{r-1} = q_1 \cdots q_{s-1}.
      Renumber so that j=sj = s, and cancel the common factor pr=qsp_r = q_s from both sides (it is non-zero).
      If r=1r = 1, then s=1s = 1 and the two factorizations are the same single prime.
      Case r=1r = 1. The left side is now the empty product 11. A product of one or more primes is at least 22, so the right side must be empty too: s=1s = 1. Then q1=p1q_1 = p_1 and we are done.
      If r≥2r \ge 2, then r−1=s−1r - 1 = s - 1 and q1,…,qs−1q_1, \dots, q_{s-1} are p1,…,pr−1p_1, \dots, p_{r-1} in some order. Put back pr=qsp_r = q_s: r=sr = s and the lists agree up to order.
      Case r≥2r \ge 2. The left side is at least 22, so the right side is not empty and s−1≥1s - 1 \ge 1. Both sides are now factorizations with r−1r - 1 and s−1s - 1 primes, so the induction hypothesis (the theorem for r−1r - 1 primes on the left) applies.
      Worked example
      Factor 360360 and 8484 into primes, and find their gcd and lcm from the factorizations. Check with Euclid.

      Solution.

      1. Factor. 360=23⋅32⋅5360 = 2^3 \cdot 3^2 \cdot 5 and 84=22⋅3⋅784 = 2^2 \cdot 3 \cdot 7. Divide out the smallest prime repeatedly: 360=8⋅45=8⋅9⋅5360 = 8 \cdot 45 = 8 \cdot 9 \cdot 5. By the theorem, this is the only factorization.
      2. The gcd takes the smaller exponent of each prime. 2min⁡(3,2)⋅3min⁡(2,1)⋅5min⁡(1,0)⋅7min⁡(0,1)=22⋅3=122^{\min(3,2)} \cdot 3^{\min(2,1)} \cdot 5^{\min(1,0)} \cdot 7^{\min(0,1)} = 2^2 \cdot 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.
      3. The lcm takes the larger exponent. 23⋅32⋅5⋅7=25202^3 \cdot 3^2 \cdot 5 \cdot 7 = 2520. The least common multiple needs each prime as often as it appears in either number.
      4. Check with Euclid. 360=4⋅84+24360 = 4 \cdot 84 + 24, 84=3⋅24+1284 = 3 \cdot 24 + 12, 24=2⋅1224 = 2 \cdot 12. So the gcd is 1212. Two methods must agree. Also 12⋅2520=30240=360⋅8412 \cdot 2520 = 30240 = 360 \cdot 84.

      Answer. gcd⁡=12\gcd = 12 and lcm⁡=2520\operatorname{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]\mathbb Z[\sqrt{-5}] of numbers a+b−5a + b\sqrt{-5} with a,ba, b integers, the number 66 factors as 2⋅32 \cdot 3 and also as (1+−5)(1−−5)(1 + \sqrt{-5})(1 - \sqrt{-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 nn (the modulus). Integers aa and bb are congruent mod nn, written a≡b(modn)a \equiv b \pmod n, if n∣(a−b)n \mid (a - b). This is the relation ∼n\sim_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}[a]_n = \{a + nk : k \in \mathbb Z\}.

      In one linea≡b(modn)a \equiv b \pmod n means nn divides a−ba - b: aa and bb leave the same remainder on division by nn.

      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 aa there is exactly one r∈{0,1,…,n−1}r \in \{0, 1, \dots, n - 1\} with a≡r(modn)a \equiv r \pmod n, namely r=a mod nr = a \bmod n. Consequently the set of classes is Zn={[0],[1],…,[n−1]}\mathbb Z_n = \{[0], [1], \dots, [n - 1]\}, with exactly nn elements.

      In one lineEvery integer is congruent mod nn to exactly one of 0,1,…,n−10, 1, \dots, n - 1, namely its remainder; so there are exactly nn classes, and a≡ba \equiv b iff aa and bb 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 nn.

      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=nqa - r = nq, a multiple of nn, and a≡ra \equiv r.
      Division algorithm: a=nq+ra = nq + r with 0≤r<n0 \le r < n.
      Then n∣r−r′n \mid r - r'.
      Uniqueness. Suppose a≡ra \equiv r and a≡r′a \equiv r' with 0≤r,r′<n0 \le r, r' < n. By symmetry and transitivity (it is an equivalence relation), r≡r′r \equiv r', that is n∣(r−r′)n \mid (r - r').
      So r=r′r = r'.
      ∣r−r′∣<n\lvert r - r' \rvert < n, and the only multiple of nn of size less than nn is 00 (fact 3).
      The classes [0],…,[n−1][0], \dots, [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][a] + [b] = [a + b] and [a][b]=[ab][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,ba, b you use.

      Theorem B

      Arithmetic respects congruence

      Let a≡a′(modn)a \equiv a' \pmod n and b≡b′(modn)b \equiv b' \pmod n. Then a+b≡a′+b′a + b \equiv a' + b', a−b≡a′−b′a - b \equiv a' - b', and ab≡a′b′ab \equiv a'b' (mod nn).

      In one lineIf a≡a′a \equiv a' and b≡b′b \equiv b' (mod nn), then a+b≡a′+b′a + b \equiv a' + b' and ab≡a′b′ab \equiv 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 nn, and those stay multiples of nn 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+nka' = a + nk and b′=b+nlb' = b + nl for integers k,lk, l.
      Definition of congruence.
      (a′+b′)−(a+b)=n(k+l)(a' + b') - (a + b) = n(k + l), so a+b≡a′+b′a + b \equiv a' + b'.
      Add the two equations. The difference is a multiple of nn.
      a′b′−ab=n(al+kb+nkl)a'b' - ab = n(al + kb + nkl), a multiple of nn, so ab≡a′b′ab \equiv a'b'.
      Expand a′b′=(a+nk)(b+nl)=ab+n(al+kb+nkl)a'b' = (a + nk)(b + nl) = ab + n(al + kb + nkl).
      (a′−b′)−(a−b)=n(k−l)(a' - b') - (a - b) = n(k - l), so a−b≡a′−b′a - b \equiv a' - b'.
      The same bookkeeping for the difference.

      So Zn\mathbb Z_n has addition and multiplication, and the usual laws (commutative, associative, distributive) hold because they hold in Z\mathbb Z: for instance [a]+[b]=[a+b]=[b+a]=[b]+[a][a] + [b] = [a + b] = [b + a] = [b] + [a]. In practice we write aa for [a][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 720257^{2025}, (b) 2100 mod 72^{100} \bmod 7, and (c) test whether 47254725 is divisible by 99 and 918082918082 by 1111.

      Solution.

      1. (a) Find a power that is 11. 72=497^2 = 49 and 74=2401≡1(mod100)7^4 = 2401 \equiv 1 \pmod{100}. If some power is ≡1\equiv 1, then powers repeat with that period, so exponents can be reduced.
      2. (a) Reduce the exponent. 2025=4⋅506+12025 = 4 \cdot 506 + 1, so 72025=(74)506⋅7≡1506⋅7=7(mod100)7^{2025} = (7^4)^{506} \cdot 7 \equiv 1^{506} \cdot 7 = 7 \pmod{100}. Arithmetic respects congruence, so we may replace 747^4 by 11 inside the product.
      3. (a) Answer. The last two digits are 0707. The remainder mod 100100 is the last two digits.
      4. (b) Same method. 23=8≡1(mod7)2^3 = 8 \equiv 1 \pmod 7 and 100=3⋅33+1100 = 3 \cdot 33 + 1, so 2100=(23)33⋅2≡2(mod7)2^{100} = (2^3)^{33} \cdot 2 \equiv 2 \pmod 7. The same pattern: find the power that gives 11.
      5. (c) Digit rule for 9. 10≡1(mod9)10 \equiv 1 \pmod 9, so 10k≡110^k \equiv 1 and a number with digits dk…d1d0d_k \dots d_1 d_0 is congruent to dk+⋯+d0d_k + \dots + d_0. For 47254725: 4+7+2+5=18≡04 + 7 + 2 + 5 = 18 \equiv 0, so 9∣47259 \mid 4725 (indeed 4725=9⋅5254725 = 9 \cdot 525). Congruence respects sums and products, so each d⋅10i≡d⋅1id \cdot 10^i \equiv d \cdot 1^i.
      6. (c) Digit rule for 11. 10≡−1(mod11)10 \equiv -1 \pmod{11}, so 10i≡(−1)i10^i \equiv (-1)^i and a number is congruent to its digits with alternating signs from the right. For 918082918082: 2−8+0−8+1−9=−22≡02 - 8 + 0 - 8 + 1 - 9 = -22 \equiv 0, so 11∣91808211 \mid 918082 (indeed 918082=11⋅83462918082 = 11 \cdot 83462). Same idea with −1-1 in place of 11.

      Answer. (a) 0707. (b) 22. (c) Both are divisible. In JEE you memorised these digit tests; they are congruences mod 99 and mod 1111.

      Watch out· You cannot always divide in modular arithmetic

      Mod 66: 2⋅3≡2⋅6≡02 \cdot 3 \equiv 2 \cdot 6 \equiv 0, but 3≢63 \not\equiv 6. So “cancel the 22” 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 bb is an inverse of aa mod nn if ab≡1(modn)ab \equiv 1 \pmod n. We write a−1a^{-1} for it. In the mod 7 table, 3−1=53^{-1} = 5 because 3⋅5=15≡13 \cdot 5 = 15 \equiv 1.

      In one lineAn inverse of aa mod nn is a number bb with ab≡1(modn)ab \equiv 1 \pmod n; it lets you divide by aa.

      Why it existsDividing by aa is multiplying by its inverse, so inverses are what make equations ax≡cax \equiv c solvable.

      Theorem B Hammack 11.3

      Inverses mod n exist exactly when coprime

      For a positive integer nn and an integer aa: aa has an inverse mod nn iff gcd⁡(a,n)=1\gcd(a, n) = 1. Any two inverses of aa are congruent mod nn.

      In one lineaa has an inverse mod nn if and only if gcd⁡(a,n)=1\gcd(a, n) = 1; the inverse is unique mod nn and is found by the extended Euclidean algorithm.

      Why it is trueAn inverse is a solution of ax+ny=1ax + ny = 1 for xx, 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=1ab - nk = 1.
      (⇒\Rightarrow) Suppose ab≡1ab \equiv 1. By the definition of congruence, ab−1=nkab - 1 = nk for some integer kk.
      Hence d∣1d \mid 1, so d=1d = 1 and gcd⁡(a,n)=1\gcd(a, n) = 1.
      Any common divisor dd of aa and nn divides ab−nkab - nk (fact 1).
      Choose x,yx, y with ax+ny=1ax + ny = 1.
      (⇐\Leftarrow) Bézout's identity: gcd⁡(a,n)=1\gcd(a, n) = 1 gives integers x,yx, y with ax+ny=1ax + ny = 1.
      So ax≡1(modn)ax \equiv 1 \pmod n, and xx is an inverse of aa.
      Then ax−1=−nyax - 1 = -ny, a multiple of nn.
      Any two inverses are congruent mod nn.
      Uniqueness. If bb and b′b' are both inverses, b≡b(ab′)=(ba)b′≡b′b \equiv b(ab') = (ba)b' \equiv b'. Each step uses that arithmetic respects congruence.
      Quick checkWhich of 44, 99, 77 has an inverse mod 1212?
      Your answerFind the inverse of 1111 mod 2626, as a number from 00 to 2525.
      Your answerHow many of the classes [0],[1],…,[35][0], [1], \dots, [35] in Z36\mathbb Z_{36} have an inverse mod 3636?
      Quick checkIn Z6\mathbb Z_6 you are told 2x≡2y(mod6)2x \equiv 2y \pmod 6. What can you conclude?
      Worked example
      Solve 7x≡4(mod40)7x \equiv 4 \pmod{40}. Also explain why 4x≡1(mod6)4x \equiv 1 \pmod 6 has no solution.

      Solution.

      1. Check that 77 is invertible. gcd⁡(7,40)=1\gcd(7, 40) = 1. The theorem: invertible iff coprime.
      2. Find the inverse with the extended Euclid. 40=5⋅7+540 = 5 \cdot 7 + 5, 7=1⋅5+27 = 1 \cdot 5 + 2, 5=2⋅2+15 = 2 \cdot 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⋅71 = 5 - 2 \cdot 2 = 5 - 2(7 - 5) = 3 \cdot 5 - 2 \cdot 7 = 3(40 - 5 \cdot 7) - 2 \cdot 7 = 3 \cdot 40 - 17 \cdot 7. This is the Bézout equation 7x+40y=17x + 40y = 1 with x=−17x = -17.
      3. Reduce. 7−1≡−17≡23(mod40)7^{-1} \equiv -17 \equiv 23 \pmod{40}. Check: 7⋅23=161=4⋅40+17 \cdot 23 = 161 = 4 \cdot 40 + 1. Add 4040 to land in the range 00 to 3939.
      4. Multiply both sides by the inverse. x≡23⋅4=92≡12(mod40)x \equiv 23 \cdot 4 = 92 \equiv 12 \pmod{40}. Multiplying both sides of a congruence by the same number keeps it true, and 23⋅7≡123 \cdot 7 \equiv 1 cancels the coefficient.
      5. Check. 7⋅12=84=2⋅40+47 \cdot 12 = 84 = 2 \cdot 40 + 4. The answer must reproduce the right side.
      6. The second question. gcd⁡(4,6)=2≠1\gcd(4, 6) = 2 \ne 1, so 44 has no inverse mod 66. Directly, 4x4x is always even and 1+6k1 + 6k is always odd, so 4x≡14x \equiv 1 is impossible. A multiple of 44 and a number of the form 6k+16k + 1 can never be equal.

      Answer. x≡12(mod40)x \equiv 12 \pmod{40} (every x=12+40tx = 12 + 40t works). The equation 4x≡1(mod6)4x \equiv 1 \pmod 6 has no solution.

      Corollary B

      Division in Z_p

      1. If pp is prime and p∤ap \nmid a, then aa has an inverse mod pp.
      2. If gcd⁡(c,n)=1\gcd(c, n) = 1 and ca≡cb(modn)ca \equiv cb \pmod n, then a≡b(modn)a \equiv b \pmod n.

      In one lineIf pp is prime, every integer not divisible by pp has an inverse mod pp; and if gcd⁡(c,n)=1\gcd(c, n) = 1, you may cancel cc: ca≡cbca \equiv cb implies a≡ba \equiv b (mod nn).

      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\gcd(a, p) = 1, so aa has an inverse by the theorem.
      gcd⁡(a,p)\gcd(a, p) is a positive divisor of pp, so it is 11 or pp. It is not pp because p∤ap \nmid a.
      Then a≡(c′c)a=c′(ca)≡c′(cb)=(c′c)b≡ba \equiv (c'c)a = c'(ca) \equiv c'(cb) = (c'c)b \equiv b.
      (2) Since gcd⁡(c,n)=1\gcd(c, n) = 1, cc has an inverse c′c' with c′c≡1c'c \equiv 1. Multiply ca≡cbca \equiv cb by c′c'.

      So Zp\mathbb Z_p for prime pp is a number system where every non-zero element can be divided by, like Q\mathbb Q or R\mathbb 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 B Fermat 1640; Euler

      Fermat's little theorem

      Let pp be a prime. If p∤ap \nmid a then ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p. For every integer aa (divisible by pp or not), ap≡a(modp)a^p \equiv a \pmod p.

      In one lineIf pp is prime and p∤ap \nmid a, then ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p; equivalently ap≡a(modp)a^p \equiv a \pmod p for every integer aa.

      Why it is trueMultiplying all the non-zero classes by aa only shuffles them, so the product of all of them is unchanged, which forces ap−1a^{p-1} to be 1.

      For p=7p = 7 and a=3a = 3 the products 3⋅1,3⋅2,…,3⋅63 \cdot 1, 3 \cdot 2, \dots, 3 \cdot 6 are 3,6,9,12,15,183, 6, 9, 12, 15, 18, which mod 77 are 3,6,2,5,1,43, 6, 2, 5, 1, 4: the numbers 11 to 66 again, in a different order (see the row of 33 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)aa, 2a, \dots, (p - 1)a is ≡0(modp)\equiv 0 \pmod p.
      By Euclid's lemma: p∤ap \nmid a and p∤ip \nmid i for 1≤i≤p−11 \le i \le p - 1, so p∤iap \nmid ia.
      The classes of a,2a,…,(p−1)aa, 2a, \dots, (p - 1)a are all different.
      Suppose ia≡jaia \equiv ja with 1≤i,j≤p−11 \le i, j \le p - 1. Then p∣(i−j)ap \mid (i - j)a. Euclid's lemma gives p∣i−jp \mid i - j (since p∤ap \nmid a).
      So i=ji = j.
      ∣i−j∣<p−1<p\lvert i - j \rvert < p - 1 \lt p, so p∣i−jp \mid i - j forces i=ji = j.
      Hence a,2a,…,(p−1)aa, 2a, \dots, (p - 1)a are congruent to 1,2,…,p−11, 2, \dots, p - 1 in some order.
      There are p−1p - 1 distinct non-zero classes, and there are exactly p−1p - 1 non-zero classes mod pp (previous theorem). So we get all of them.
      ap−1⋅(p−1)!≡(p−1)!(modp)a^{p-1} \cdot (p - 1)! \equiv (p - 1)! \pmod p.
      Multiply all the congruences together (arithmetic respects congruence); the order of multiplication does not matter.
      So ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p.
      (p−1)!(p - 1)! is a product of numbers not divisible by pp, so by Euclid's lemma p∤(p−1)!p \nmid (p-1)!, thus gcd⁡((p−1)!,p)=1\gcd((p-1)!, p) = 1 and we may cancel it (the corollary).
      Therefore ap≡a(modp)a^p \equiv a \pmod p for all aa.
      Multiply by aa. If p∣ap \mid a, both sides of ap≡aa^p \equiv a are ≡0\equiv 0.
      Quick checkWhat is 7100 mod 117^{100} \bmod 11?
      Your answerWhat is 3100 mod 73^{100} \bmod 7 (a number from 00 to 66)?
      Worked example
      Compute 5117 mod 115^{117} \bmod 11, and find 3−1 mod 73^{-1} \bmod 7 using Fermat.

      Solution.

      1. Use Fermat. 1111 is prime and 11∤511 \nmid 5, so 510≡1(mod11)5^{10} \equiv 1 \pmod{11}. The exponent p−1=10p - 1 = 10 is the period of the powers.
      2. Reduce the exponent mod 10. 117=11⋅10+7117 = 11 \cdot 10 + 7, so 5117=(510)11⋅57≡575^{117} = (5^{10})^{11} \cdot 5^7 \equiv 5^7. Replace 5105^{10} by 11.
      3. Compute 575^7 by squaring. 52=25≡35^2 = 25 \equiv 3, 54≡32=95^4 \equiv 3^2 = 9, so 57=54⋅52⋅5≡9⋅3⋅5=135=12⋅11+35^7 = 5^4 \cdot 5^2 \cdot 5 \equiv 9 \cdot 3 \cdot 5 = 135 = 12 \cdot 11 + 3. 7=4+2+17 = 4 + 2 + 1, and each power is reduced mod 1111 as we go.
      4. The inverse. 36≡1(mod7)3^6 \equiv 1 \pmod 7, so 3⋅35≡13 \cdot 3^5 \equiv 1 and 3−1≡35=243=34⋅7+5≡53^{-1} \equiv 3^5 = 243 = 34 \cdot 7 + 5 \equiv 5. In general a−1≡ap−2(modp)a^{-1} \equiv a^{p-2} \pmod p.

      Answer. 5117≡3(mod11)5^{117} \equiv 3 \pmod{11}, and 3−1≡5(mod7)3^{-1} \equiv 5 \pmod 7 (check: 3⋅5=15≡13 \cdot 5 = 15 \equiv 1).

      Watch out· Fermat's theorem needs p prime and p not dividing a

      The converse is false. The number 341=11⋅31341 = 11 \cdot 31 is composite, yet 2340≡1(mod341)2^{340} \equiv 1 \pmod{341}. Such numbers are why primality tests use more than Fermat. Also, an exponent may be reduced mod p−1p - 1 only when pp does not divide the base. The base itself is reduced mod pp.

      Why RSA works

      RSA needs one more step: Fermat’s theorem for a product of two primes.

      Theorem B

      RSA decryption works

      Let p≠qp \ne q be primes, n=pqn = pq, and e,de, d positive integers with ed=1+k(p−1)(q−1)ed = 1 + k(p-1)(q-1) for some integer k≥0k \ge 0. Then med≡m(modn)m^{ed} \equiv m \pmod n for every integer mm.

      In one lineIf n=pqn = pq for distinct primes and ed≡1(mod(p−1)(q−1))ed \equiv 1 \pmod{(p-1)(q-1)}, then (me)d≡m(modn)(m^e)^d \equiv m \pmod n for every integer mm.

      Why it is trueFermat's theorem shows med≡mm^{ed} \equiv m separately mod pp and mod qq, 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 pp, first suppose p∣mp \mid m. Then both medm^{ed} and mm are ≡0\equiv 0.
      Case p∣mp \mid m: then pp divides medm^{ed} and mm, so p∣med−mp \mid m^{ed} - m.
      If p∤mp \nmid m, then med=m⋅(mp−1)k(q−1)≡m(modp)m^{ed} = m \cdot (m^{p-1})^{k(q-1)} \equiv m \pmod p.
      Case p∤mp \nmid m: Fermat gives mp−1≡1m^{p-1} \equiv 1. Then med=m⋅(mp−1)k(q−1)≡m⋅1=mm^{ed} = m \cdot (m^{p-1})^{k(q-1)} \equiv m \cdot 1 = m.
      Likewise med≡m(modq)m^{ed} \equiv m \pmod q.
      The identical argument with qq in place of pp, using ed=1+k(q−1)(p−1)ed = 1 + k(q-1)(p-1).
      So p∣med−mp \mid m^{ed} - m and q∣med−mq \mid m^{ed} - m, and hence pq∣med−mpq \mid m^{ed} - m.
      Write med−m=ptm^{ed} - m = pt. Then q∣ptq \mid pt. The prime qq does not divide the different prime pp, so Euclid's lemma gives q∣tq \mid t.
      So med≡m(modn)m^{ed} \equiv m \pmod n.
      That is the definition of congruence mod nn.
      Where this came from· Gauss, Diffie and Hellman, and RSA

      Gauss’s Disquisitiones Arithmeticae (1801) gave the notation a≡b(modn)a \equiv b \pmod n, 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 nn, 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][3] + [4] = [1] in Z6\mathbb Z_6? Find all pairs (a,b)(a, b) of non-zero classes in Z6\mathbb Z_6 with ab=0ab = 0.

      Solution.

      1. The sum. 3+4=7=6+13 + 4 = 7 = 6 + 1, so [3]+[4]=[1][3] + [4] = [1] in Z6\mathbb Z_6. Reduce the sum mod 66.
      2. Hunt for zero products. A product ab≡0(mod6)ab \equiv 0 \pmod 6 means 6∣ab6 \mid ab. Try a=2a = 2: 2⋅3=62 \cdot 3 = 6, so [2][3]=0[2][3] = 0. Try a=4a = 4: 4⋅3=124 \cdot 3 = 12, so [4][3]=0[4][3] = 0. Also [2][3]=[3][2]=0[2][3] = [3][2] = 0 and [3][4]=0[3][4] = 0. Look for pairs whose prime factors 22 and 33 are split between them.
      3. Sanity check with the table. In the mod 6 table, the zeros are at (2,3),(3,2),(3,4),(4,3)(2,3), (3,2), (3,4), (4,3). The table agrees.

      Answer. Yes. The non-zero pairs with product zero are {2,3}\{2, 3\} and {3,4}\{3, 4\} (in either order). These “zero divisors” exist exactly because 66 is not prime, which is why Z6\mathbb Z_6 cannot divide.

      Worked example
      The first twelve digits of an ISBN-13 are 978140289462978140289462. Find the check digit, and say whether 97801311036279780131103627 is a valid ISBN-13.

      The rule: multiply the digits alternately by 11 and 33, starting with 11, and add. A valid ISBN-13 has total ≡0(mod10)\equiv 0 \pmod{10}. The last digit is chosen to make that true.

      Solution.

      1. 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⋅39\cdot1 + 7\cdot3 + 8\cdot1 + 1\cdot3 + 4\cdot1 + 0\cdot3 + 2\cdot1 + 8\cdot3 + 9\cdot1 + 4\cdot3 + 6\cdot1 + 2\cdot3. The weights run 1,3,1,3,…1, 3, 1, 3, \dots across the twelve digits, and the thirteenth digit has weight 11.
      2. Add. 9+21+8+3+4+0+2+24+9+12+6+6=1049 + 21 + 8 + 3 + 4 + 0 + 2 + 24 + 9 + 12 + 6 + 6 = 104. Plain arithmetic.
      3. Choose the check digit cc. We need 104+c≡0(mod10)104 + c \equiv 0 \pmod{10}, and 104≡4104 \equiv 4, so c≡−4≡6c \equiv -4 \equiv 6. The check digit is the number from 00 to 99 that completes the congruence.
      4. Test the second number. Its first twelve digits 978013110362978013110362 give 9+21+8+0+1+9+1+3+0+9+6+6=739 + 21 + 8 + 0 + 1 + 9 + 1 + 3 + 0 + 9 + 6 + 6 = 73, and adding the last digit 77 with weight 11 gives 80≡080 \equiv 0. The full total, including the check digit, must be a multiple of 1010.

      Answer. The check digit is 66, and 97801311036279780131103627 is valid. Changing any single digit changes the total by w⋅ew \cdot e with w∈{1,3}w \in \{1, 3\}, which is never ≡0(mod10)\equiv 0 \pmod{10} because 11 and 33 are invertible mod 1010, 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.

      1. Assume the opposite. Suppose the primes are exactly p1,p2,…,pkp_1, p_2, \dots, p_k. Proof by contradiction (0.2): we assume the negation of the claim.
      2. Build a number. Let N=p1p2⋯pk+1N = p_1 p_2 \cdots p_k + 1. Then N>1N > 1. Every pip_i divides the product, so dividing NN by pip_i always leaves remainder 11.
      3. Find a prime divisor. By the fundamental theorem NN is a product of primes, so some prime pp divides NN. Since the list contains all primes, p=pip = p_i for some ii. Existence of a prime factorization, applied to NN.
      4. Get the contradiction. Then pi∣Np_i \mid N and pi∣p1⋯pkp_i \mid p_1 \cdots p_k, so pi∣N−p1⋯pk=1p_i \mid N - p_1 \cdots p_k = 1. But no prime divides 11. Fact 1: a divisor of two numbers divides their difference. A prime is greater than 11, and a divisor of 11 has size at most 11 (fact 3).

      Answer. The assumption is impossible, so there are infinitely many primes. The proof does not claim that NN itself is prime.

      Worked example
      Solve the system x≡3(mod4)x \equiv 3 \pmod 4 and x≡2(mod7)x \equiv 2 \pmod 7.

      Solution.

      1. Check the moduli are coprime. gcd⁡(4,7)=1\gcd(4, 7) = 1. Then Bézout gives 4u+7v=14u + 7v = 1, which is the tool for gluing the two conditions.
      2. Find u,vu, v. 4⋅2+7⋅(−1)=14 \cdot 2 + 7 \cdot (-1) = 1, so u=2u = 2, v=−1v = -1. Extended Euclid, or just trial for numbers this small.
      3. Build a solution. Take x0=3⋅7⋅(−1)+2⋅4⋅2=−21+16=−5x_0 = 3 \cdot 7 \cdot (-1) + 2 \cdot 4 \cdot 2 = -21 + 16 = -5. The term 7v≡1(mod4)7v \equiv 1 \pmod 4 and 4u≡1(mod7)4u \equiv 1 \pmod 7. So 7v7v acts like 11 mod 44 and like 00 mod 77, and 4u4u acts like 00 mod 44 and like 11 mod 77. The combination 3⋅(7v)+2⋅(4u)3 \cdot (7v) + 2 \cdot (4u) is then 33 mod 44 and 22 mod 77.
      4. Reduce. −5+28=23-5 + 28 = 23. Adding the product of the moduli, 2828, changes neither congruence.
      5. Check. 23=5⋅4+323 = 5 \cdot 4 + 3 and 23=3⋅7+223 = 3 \cdot 7 + 2. The answer must reproduce both remainders.

      Answer. x≡23(mod28)x \equiv 23 \pmod{28}, and every solution has this form.

      Where it is used

      Used inside maths· Inside maths: fields, groups, and the Euclidean algorithm for polynomials

      Zp\mathbb Z_p for a prime pp is a finite field, so you can do linear algebra over it: vector spaces, bases, and determinants all work with scalars from Zp\mathbb Z_p (chapter 2.1 uses fields). The invertible classes mod nn form a group under multiplication, with φ(n)\varphi(n) elements (the number of kk from 11 to nn with gcd⁡(k,n)=1\gcd(k, n) = 1). Lagrange’s theorem (MATH 541) then gives Euler’s generalisation of Fermat: aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod n when gcd⁡(a,n)=1\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=5p = 5 and q=11q = 11. Then n=pq=55n = pq = 55 and (p−1)(q−1)=4⋅10=40(p-1)(q-1) = 4 \cdot 10 = 40. Choose e=3e = 3, which is coprime to 4040. The private exponent dd is the inverse of 33 mod 4040. Publish (n,e)=(55,3)(n, e) = (55, 3) and keep dd secret.

      Your answerTake the RSA primes p=7p = 7 and q=11q = 11 with public exponent e=7e = 7. What is the private exponent dd, as the smallest positive number with ed≡1(mod(p−1)(q−1))ed \equiv 1 \pmod{(p-1)(q-1)}?

      Real RSA uses primes of about 1024 bits each, so nn has about 617 digits, and a common public exponent is e=65 537e = 65\,537. Here anyone can factor 55=5⋅1155 = 5 \cdot 11 at once, compute 4040, and invert 33. With a 617-digit nn 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 11 and 33 and add; the total must be ≡0(mod10)\equiv 0 \pmod{10}. For 978-0-306-40615-7978\text{-}0\text{-}306\text{-}40615\text{-}7 the sum is 9+21+8+0+3+0+6+12+0+18+1+15+7=1009 + 21 + 8 + 0 + 3 + 0 + 6 + 12 + 0 + 18 + 1 + 15 + 7 = 100. A single wrong digit changes the sum by w⋅ew \cdot e with weight w∈{1,3}w \in \{1, 3\} and error 0<∣e∣≤90 < \lvert e \rvert \le 9. Both weights are coprime to 1010, so they are invertible, and we≡0we \equiv 0 would force e≡0(mod10)e \equiv 0 \pmod{10}, which is impossible. So every single-digit error is caught. Swapping two adjacent digits x,yx, y changes the sum by 2(x−y)2(x - y), which is ≡0\equiv 0 only when x−y=±5x - y = \pm 5, so swaps like 0↔50 \leftrightarrow 5 slip through.
      • ISBN-10: weights 10,9,…,110, 9, \dots, 1 and modulus 1111. For 0-306-40615-20\text{-}306\text{-}40615\text{-}2 the sum is 132=11⋅12132 = 11 \cdot 12. Because 1111 is prime, every weight is invertible and the difference of two different digits is never a multiple of 1111, so every single-digit error and every swap of adjacent different digits is caught. (The check digit may be 1010, written X.)
      • UPC-A (barcodes): weights 3,1,3,1,…3, 1, 3, 1, \dots on the 12 digits, total ≡0(mod10)\equiv 0 \pmod{10}. For 036000291452036000291452: 3(0+6+0+2+1+5)+(3+0+0+9+4+2)=42+18=603(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 99 if the result exceeds 99), add everything, and require a multiple of 1010. For 7992739871379927398713 the processed digits are 7,9,9,4,7,6,9,7,7,2,37, 9, 9, 4, 7, 6, 9, 7, 7, 2, 3 with sum 7070. The doubling map sends the digits 0,1,…,90, 1, \dots, 9 to 0,2,4,6,8,1,3,5,7,90, 2, 4, 6, 8, 1, 3, 5, 7, 9, a shuffle of the digits, so changing one digit always changes the sum mod 1010. It catches every adjacent swap except 09↔9009 \leftrightarrow 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)=k mod mh(k) = k \bmod m puts key kk in one of mm buckets. Take the ten keys 100,110,…,190100, 110, \dots, 190. With m=10m = 10 every key lands in bucket 00, since each is a multiple of 1010. With m=11m = 11 they land in buckets 1,0,10,9,8,7,6,5,4,31, 0, 10, 9, 8, 7, 6, 5, 4, 3, all different, because 1010 is coprime to 1111. In general, if every key is a multiple of gg and gg divides mm, only m/gm / 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 mm buckets can avoid collisions once there are more than mm keys.

      Traps

      Watch out· Remainders are never negative

      −7 mod 3=2-7 \bmod 3 = 2, not −1-1. Some programming languages (C, Java) return −1-1 for the remainder of −7/3-7 / 3 and others (Python) return 22. In mathematics the remainder is in 0,…,n−10, \dots, n - 1. The two are congruent mod 33, 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

      a mod na \bmod n is a number, the remainder of aa. a≡b(modn)a \equiv b \pmod n is a statement about aa and bb. We have 17≡2(mod5)17 \equiv 2 \pmod 5 and 17 mod 5=217 \bmod 5 = 2, but 17 mod 5=217 \bmod 5 = 2 is an equation between numbers while the first line is a relation.

      Watch out· Cancelling needs a unit

      You may cancel cc from ca≡cb(modn)ca \equiv cb \pmod n only when gcd⁡(c,n)=1\gcd(c, n) = 1. In general you can only cancel to a≡b(modn/gcd⁡(c,n))a \equiv b \pmod{n / \gcd(c, n)}. For example 2⋅1≡2⋅4(mod6)2 \cdot 1 \equiv 2 \cdot 4 \pmod 6 gives 1≡4(mod3)1 \equiv 4 \pmod 3, which is true, not 1≡4(mod6)1 \equiv 4 \pmod 6.

      Watch out· Reducing exponents

      Exponents reduce mod p−1p - 1 (when p∤ap \nmid a), not mod pp: ap−1≡1a^{p-1} \equiv 1, so aka^{k} depends on k mod (p−1)k \bmod (p-1). For p=7p = 7: 38≡32=9≡23^{8} \equiv 3^{2} = 9 \equiv 2, not 38 mod 7=313^{8 \bmod 7} = 3^1.

      Watch out· The number 1 is not prime

      The integer 11 is not prime, and negative numbers are not called prime in this book. A prime has exactly two positive divisors, which is why 11 (one divisor) is excluded. Without this, unique factorization would fail (6=2⋅3=1⋅2⋅36 = 2 \cdot 3 = 1 \cdot 2 \cdot 3).

      Problems

      check

      Find the quotient and remainder when 10001000 is divided by 77, and when −45-45 is divided by 88.

      Solution

      1000=7⋅142+61000 = 7 \cdot 142 + 6, so q=142q = 142, r=6r = 6. −45=8⋅(−6)+3-45 = 8 \cdot (-6) + 3, so q=−6q = -6, r=3r = 3 (check: −48+3=−45-48 + 3 = -45).

      check

      Compute gcd⁡(126,84)\gcd(126, 84) by Euclid’s algorithm, and gcd⁡(91,35)\gcd(91, 35).

      Solution

      126=1⋅84+42126 = 1 \cdot 84 + 42, 84=2⋅42+084 = 2 \cdot 42 + 0, so gcd⁡(126,84)=42\gcd(126, 84) = 42. 91=2⋅35+2191 = 2 \cdot 35 + 21, 35=1⋅21+1435 = 1 \cdot 21 + 14, 21=1⋅14+721 = 1 \cdot 14 + 7, 14=2⋅714 = 2 \cdot 7, so gcd⁡(91,35)=7\gcd(91, 35) = 7.

      check

      Does 2x≡3(mod6)2x \equiv 3 \pmod 6 have a solution? Does 5x≡1(mod12)5x \equiv 1 \pmod{12}?

      Solution

      gcd⁡(2,6)=2∤3\gcd(2, 6) = 2 \nmid 3, so the first has no solution (2x−32x - 3 is odd, never a multiple of 66). gcd⁡(5,12)=1\gcd(5, 12) = 1, and 5⋅5=25=2⋅12+15 \cdot 5 = 25 = 2 \cdot 12 + 1, so x≡5x \equiv 5 is a solution.

      checkCS 240

      Find 350 mod 53^{50} \bmod 5 and the last digit of 71007^{100}.

      Solution

      34=81≡1(mod5)3^4 = 81 \equiv 1 \pmod 5, 50=4⋅12+250 = 4 \cdot 12 + 2, so 350≡32=9≡43^{50} \equiv 3^2 = 9 \equiv 4. For the last digit take mod 1010: 74=2401≡1(mod10)7^4 = 2401 \equiv 1 \pmod{10} and 100=4⋅25100 = 4 \cdot 25, so 7100≡17^{100} \equiv 1. The last digit is 11.

      use

      Find integers x,yx, y with 91x+35y=791x + 35y = 7.

      Solution

      From the Euclid lines 91=2⋅35+2191 = 2 \cdot 35 + 21, 35=1⋅21+1435 = 1 \cdot 21 + 14, 21=1⋅14+721 = 1 \cdot 14 + 7: 7=21−14=21−(35−21)=2⋅21−35=2(91−2⋅35)−35=2⋅91−5⋅357 = 21 - 14 = 21 - (35 - 21) = 2 \cdot 21 - 35 = 2(91 - 2 \cdot 35) - 35 = 2 \cdot 91 - 5 \cdot 35. So x=2x = 2, y=−5y = -5. Check: 182−175=7182 - 175 = 7.

      use

      Solve 17x≡5(mod43)17x \equiv 5 \pmod{43}.

      Solution

      gcd⁡(17,43)=1\gcd(17, 43) = 1. Euclid: 43=2⋅17+943 = 2 \cdot 17 + 9, 17=1⋅9+817 = 1 \cdot 9 + 8, 9=1⋅8+19 = 1 \cdot 8 + 1. Back: 1=9−8=9−(17−9)=2⋅9−17=2(43−2⋅17)−17=2⋅43−5⋅171 = 9 - 8 = 9 - (17 - 9) = 2 \cdot 9 - 17 = 2(43 - 2 \cdot 17) - 17 = 2 \cdot 43 - 5 \cdot 17. So 17−1≡−5≡3817^{-1} \equiv -5 \equiv 38. Then x≡5⋅38=190=4⋅43+18≡18x \equiv 5 \cdot 38 = 190 = 4 \cdot 43 + 18 \equiv 18. Check: 17⋅18=306=7⋅43+517 \cdot 18 = 306 = 7 \cdot 43 + 5.

      use

      Use Fermat’s theorem to find 2100 mod 132^{100} \bmod 13.

      Solution

      1313 is prime and 13∤213 \nmid 2, so 212≡1(mod13)2^{12} \equiv 1 \pmod{13}. 100=8⋅12+4100 = 8 \cdot 12 + 4, so 2100≡24=16≡32^{100} \equiv 2^4 = 16 \equiv 3.

      use

      RSA with p=7p = 7, q=13q = 13, e=5e = 5. Find nn, the private exponent dd, and encrypt m=10m = 10. Check that decryption returns 1010.

      Solution

      n=91n = 91 and (p−1)(q−1)=6⋅12=72(p-1)(q-1) = 6 \cdot 12 = 72. gcd⁡(5,72)=1\gcd(5, 72) = 1. Find dd: 72=14⋅5+272 = 14 \cdot 5 + 2, 5=2⋅2+15 = 2 \cdot 2 + 1, so 1=5−2⋅2=5−2(72−14⋅5)=29⋅5−2⋅721 = 5 - 2 \cdot 2 = 5 - 2(72 - 14 \cdot 5) = 29 \cdot 5 - 2 \cdot 72, so d=29d = 29 (5⋅29=145=2⋅72+15 \cdot 29 = 145 = 2 \cdot 72 + 1). Encrypt: 105=100000=1098⋅91+8210^5 = 100000 = 1098 \cdot 91 + 82, so c=82c = 82. Decrypt: 8229 mod 91=1082^{29} \bmod 91 = 10 (check by repeated squaring, or by Fermat mod 77 and mod 1313 separately, as in the proof).

      use

      Is the ISBN-13 97803064061589780306406158 valid? What should its last digit be?

      Solution

      Weights 1,3,1,3,…1, 3, 1, 3, \dots on the first twelve digits 9,7,8,0,3,0,6,4,0,6,1,59, 7, 8, 0, 3, 0, 6, 4, 0, 6, 1, 5 give 9+21+8+0+3+0+6+12+0+18+1+15=939 + 21 + 8 + 0 + 3 + 0 + 6 + 12 + 0 + 18 + 1 + 15 = 93. The check digit must make the total a multiple of 1010, so it is 77. The last digit 88 gives 101101, which is not, so the number is invalid.

      proveHammack 5.4

      Prove that gcd⁡(a,b)=gcd⁡(a,b+ka)\gcd(a, b) = \gcd(a, b + ka) for every integer kk (with aa and bb not both zero).

      Solution

      Let dd divide aa and bb. Then dd divides b+kab + ka by fact 1, so every common divisor of (a,b)(a, b) is a common divisor of (a,b+ka)(a, b + ka). Conversely if dd divides aa and b+kab + ka then dd divides (b+ka)−ka=b(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\gcd(a, n) = 1 and gcd⁡(b,n)=1\gcd(b, n) = 1 then gcd⁡(ab,n)=1\gcd(ab, n) = 1.

      Solution

      By Bézout, ax+ny=1ax + ny = 1 and bu+nv=1bu + nv = 1 for integers x,y,u,vx, y, u, v. Multiply: 1=(ax+ny)(bu+nv)=ab(xu)+n(axv+ybu+nyv)1 = (ax + ny)(bu + nv) = ab(xu) + n(axv + ybu + nyv). This is a combination of abab and nn equal to 11, so any common divisor of abab and nn divides 11, and gcd⁡(ab,n)=1\gcd(ab, n) = 1.

      prove

      Prove: if a∣ca \mid c, b∣cb \mid c and gcd⁡(a,b)=1\gcd(a, b) = 1, then ab∣cab \mid c. Is it true without the coprime condition?

      Solution

      Write c=akc = ak and c=blc = bl, and take ax+by=1ax + by = 1 (Bézout). Then c=cax+cby=(bl)ax+(ak)by=ab(lx+ky)c = cax + cby = (bl)ax + (ak)by = ab(lx + ky), so ab∣cab \mid c. Without coprimality it fails: 6∣126 \mid 12 and 4∣124 \mid 12 but 24∤1224 \nmid 12.

      proveEuclid IX.20

      Prove that for every positive integer nn there is a prime p>np > n. (Look at n!+1n! + 1.)

      Solution

      The number N=n!+1N = n! + 1 is greater than 11, so it has a prime divisor pp by the fundamental theorem. Suppose p≤np \le n. Then pp is one of the factors 1,2,…,n1, 2, \dots, n of n!n!, so p∣n!p \mid n!. Also p∣Np \mid N, so p∣N−n!=1p \mid N - n! = 1 by fact 1, which is impossible for a prime. So p>np > n. (Hence there is no largest prime.)

      prove

      Prove: gcd⁡(a,b)⋅lcm⁡(a,b)=ab\gcd(a, b) \cdot \operatorname{lcm}(a, b) = ab for positive integers, using the prime factorizations of aa and bb.

      Solution

      Write a=∏pisia = \prod p_i^{s_i} and b=∏pitib = \prod p_i^{t_i} over the same list of primes, allowing exponents 00. By unique factorization the gcd is ∏pimin⁡(si,ti)\prod p_i^{\min(s_i, t_i)} and the lcm is ∏pimax⁡(si,ti)\prod p_i^{\max(s_i, t_i)} (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 ii, min⁡(si,ti)+max⁡(si,ti)=si+ti\min(s_i, t_i) + \max(s_i, t_i) = s_i + t_i, so the product of gcd and lcm has exponent si+tis_i + t_i on pip_i, which is the exponent of abab. Example: 12⋅2520=30240=360⋅8412 \cdot 2520 = 30240 = 360 \cdot 84.

      stretch

      Prove gcd⁡(2m−1, 2n−1)=2gcd⁡(m,n)−1\gcd(2^m - 1,\ 2^n - 1) = 2^{\gcd(m, n)} - 1, and check it for m=12m = 12, n=18n = 18.

      Solution

      Let m=nq+rm = nq + r with 0≤r<n0 \le r < n. Then 2m−1=2r(2nq−1)+(2r−1)2^m - 1 = 2^r(2^{nq} - 1) + (2^r - 1). Since x−1x - 1 divides xq−1x^q - 1 (geometric sum), 2n−12^n - 1 divides 2nq−12^{nq} - 1, so 2m−1=(2n−1)t+(2r−1)2^m - 1 = (2^n - 1)t + (2^r - 1) for an integer tt. By Euclid’s reduction step, gcd⁡(2m−1,2n−1)=gcd⁡(2n−1,2r−1)\gcd(2^m - 1, 2^n - 1) = \gcd(2^n - 1, 2^r - 1). This is Euclid’s algorithm run on the exponents m,n,r,…m, n, r, \dots, so it ends with gcd⁡(2g−1,20−1)=gcd⁡(2g−1,0)=2g−1\gcd(2^g - 1, 2^0 - 1) = \gcd(2^g - 1, 0) = 2^g - 1, where g=gcd⁡(m,n)g = \gcd(m, n) is the last non-zero exponent. For m=12m = 12, n=18n = 18: g=6g = 6 and 26−1=632^6 - 1 = 63. Direct: 212−1=4095=63⋅652^{12} - 1 = 4095 = 63 \cdot 65 and 218−1=262143=63⋅41612^{18} - 1 = 262143 = 63 \cdot 4161, with gcd⁡(65,4161)=1\gcd(65, 4161) = 1.

      stretchChinese remainder theorem

      Let gcd⁡(m,n)=1\gcd(m, n) = 1. Prove that for any integers a,ba, b the system x≡a(modm)x \equiv a \pmod m, x≡b(modn)x \equiv b \pmod n has a solution, and that it is unique mod mnmn. Solve x≡2(mod3)x \equiv 2 \pmod 3, x≡3(mod5)x \equiv 3 \pmod 5.

      Solution

      By Bézout, mu+nv=1mu + nv = 1 for some integers u,vu, v. Put x0=anv+bmux_0 = a n v + b m u. Mod mm: nv=1−mu≡1nv = 1 - mu \equiv 1, and bmu≡0bmu \equiv 0, so x0≡ax_0 \equiv a. Mod nn: mu=1−nv≡1mu = 1 - nv \equiv 1 and anv≡0anv \equiv 0, so x0≡bx_0 \equiv b. Uniqueness: if xx and x′x' are both solutions then m∣x−x′m \mid x - x' and n∣x−x′n \mid x - x', so mn∣x−x′mn \mid x - x' by the coprime problem above. For m=3m = 3, n=5n = 5: 3⋅2+5⋅(−1)=13 \cdot 2 + 5 \cdot (-1) = 1, so u=2u = 2, v=−1v = -1 and x0=2⋅5⋅(−1)+3⋅3⋅2=−10+18=8x_0 = 2 \cdot 5 \cdot (-1) + 3 \cdot 3 \cdot 2 = -10 + 18 = 8. Check: 8=3⋅2+2=5+38 = 3 \cdot 2 + 2 = 5 + 3. All solutions are x≡8(mod15)x \equiv 8 \pmod{15}.

      Summary

      • Division algorithm: for b>0b > 0, a=bq+ra = bq + r with 0≤r<b0 \le r < b, qq and rr unique (proved by well-ordering). Remainders are never negative.
      • Euclid’s algorithm replaces (a,b)(a, b) by (b,r)(b, r) without changing the gcd, and ends at the last non-zero remainder.
      • Bézout: gcd⁡(a,b)=ax+by\gcd(a, b) = ax + by for some integers x,yx, y (the extended Euclid finds them). Hence ax+by=cax + by = c is solvable iff gcd⁡(a,b)∣c\gcd(a, b) \mid c.
      • Euclid’s lemma: a prime dividing a product divides a factor. It gives unique factorization into primes.
      • a≡b(modn)a \equiv b \pmod n means n∣a−bn \mid a - b. It is an equivalence relation with exactly nn classes, and ++ and ×\times on classes are well defined, as for fractions in 0.4.
      • Inverses: aa has an inverse mod nn iff gcd⁡(a,n)=1\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)a^{p-1} \equiv 1 \pmod p for p∤ap \nmid a. It makes RSA work: encryption is mem^e, decryption cdc^d, with ed≡1(mod(p−1)(q−1))ed \equiv 1 \pmod{(p-1)(q-1)}, and the security is the difficulty of factoring n=pqn = 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.

      Where this sits

      Comes from
      0.2 Proof techniques
      0.4 Equivalence relations and partitions
      Leads to
      2.1 Vector spaces and subspaces
      Same idea elsewhere
      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.