The Timpson Lyceum seal

The Timpson Lyceum · Discrete Math (2025)

Number Theory, GCD, and Modular Arithmetic - Lecture 19 (Discrete Math)

Overview

This lecture builds the core machinery of elementary number theory. Starting from the Division Algorithm (every pair of integers has a unique quotient and remainder), we define the greatest common divisor (GCD) and develop Euclid's algorithm, an astonishingly fast way to compute it — roughly 7,000 steps for numbers as large as 1010010^{100}, versus the hopeless task of testing every candidate divisor. We then prove that the GCD of aa and bb is the smallest positive value of ax+byax + by, which leads to the Extended Euclidean algorithm for finding those coefficients xx and yy. Finally, we introduce modular arithmetic on the finite sets Zn\mathbb{Z}_n — clock-style addition, multiplication, and subtraction — and see why division is the one operation that resists an easy definition.

Key Concepts

Division Algorithm (recap). For any integers aa and bb (with b>0b > 0), there exist a unique quotient qq and remainder rr with a=qb+r,0≤r<b.a = qb + r, \qquad 0 \le r < b. The remainder must satisfy 0≤r<b0 \le r < b; you can't say "1010 goes into 2727 once remainder 1717" — the correct statement is twice remainder 77.

Divisor and common divisor. If d∣ad \mid a and d∣bd \mid b, then dd is a common divisor of aa and bb. Example: 1,2,41, 2, 4 are the positive common divisors of 1616 and 2828.

Greatest common divisor. d=gcd⁡(a,b)d = \gcd(a, b) if dd is a common divisor of aa and bb and, for every other common divisor ee, we have e≤de \le d. We treat gcd⁡\gcd as a function: gcd⁡(16,28)=4\gcd(16, 28) = 4.

The key fact behind Euclid's algorithm. Let c=a mod bc = a \bmod b (the remainder). Then gcd⁡(a,b)=gcd⁡(b,c).\gcd(a, b) = \gcd(b, c). Why it works: writing a=qb+ca = qb + c, any divisor of aa and bb also divides c=a−qbc = a - qb, and any divisor of bb and cc also divides a=qb+ca = qb + c. So {a,b}\{a,b\} and {b,c}\{b,c\} have exactly the same common divisors, hence the same GCD. (Formally: let d=gcd⁡(a,b)d=\gcd(a,b), e=gcd⁡(b,c)e=\gcd(b,c); show d≤ed \le e and e≤de \le d, so d=ed=e.)

Euclid's algorithm. Repeatedly replace (a,b)(a, b) with (b, a mod b)(b,\, a \bmod b) until the remainder is 00. The last nonzero remainder is the GCD. This terminates fast because gcd⁡(d,0)=d\gcd(d, 0) = d (every integer divides 00).

GCD as a smallest positive combination. For integers a,ba, b not both zero, the smallest positive value of ax+byax + by over all integers x,yx, y is exactly gcd⁡(a,b)\gcd(a, b). The set D={ax+by:x,y∈Z, ax+by>0}D = \{ax + by : x,y \in \mathbb{Z},\ ax+by > 0\} is a nonempty subset of N\mathbb{N} (it contains a⋅a+b⋅b>0a\cdot a + b\cdot b > 0), so by the well-ordering principle it has a least element dd; one shows d∣ad \mid a, d∣bd \mid b, and that any common divisor ee divides the combination dd, forcing e≤de \le d.

Extended Euclidean algorithm. Runs Euclid's algorithm, then back-substitutes to express the GCD as ax+byax + by. At each division step solve for the remainder, then work back up, eliminating remainders one at a time until the GCD is written in terms of the original aa and bb.

Relatively prime. Two integers are relatively prime when gcd⁡(a,b)=1\gcd(a, b) = 1. They need not be prime themselves — e.g. gcd⁡(16,25)=1\gcd(16, 25) = 1. Consecutive integers are always relatively prime.

Corollary. aa and bb are relatively prime iff there exist integers x,yx, y with ax+by=1ax + by = 1.

Modular arithmetic. Define Zn={0,1,2,…,n−1}\mathbb{Z}_n = \{0, 1, 2, \ldots, n-1\} (for n>1n > 1). Arithmetic "wraps around" like a clock that starts at 00: a⊕b=(a+b) mod n,a⊗b=(a⋅b) mod n.a \oplus b = (a + b) \bmod n, \qquad a \otimes b = (a \cdot b) \bmod n. Both operations are closed on Zn\mathbb{Z}_n (the remainder is always in range), commutative, associative, with additive identity 00 and multiplicative identity 11, and they distribute. (Zn\mathbb{Z}_n under ⊕\oplus is a group.)

Congruence. a≡b(modn)a \equiv b \pmod{n} means n∣(a−b)n \mid (a - b), which is equivalent to (a mod n)=(b mod n)(a \bmod n) = (b \bmod n).

Modular subtraction. a⊖ba \ominus b is the unique x∈Znx \in \mathbb{Z}_n with b⊕x=ab \oplus x = a; it is computed as (a−b) mod n(a - b) \bmod n. For example in Z12\mathbb{Z}_{12}, 1−3=−2≡101 - 3 = -2 \equiv 10 (3 hours before 1 o'clock is 10 o'clock).

Why division is hard (preview). In Z10\mathbb{Z}_{10}, 4÷24 \div 2 would need an xx with 2x=42x = 4; both x=2x = 2 and x=7x = 7 work (2⋅7=14≡42\cdot 7 = 14 \equiv 4), so the answer isn't unique. A multiplicative inverse exists only when the numbers are relatively prime to nn — handled next time.

Worked Example

Extended Euclidean algorithm: find x,yx, y with 689x+234y=gcd⁡(689,234)689x + 234y = \gcd(689, 234).

Run Euclid's algorithm (left), recording each remainder (right):

689=2⋅234+221⇒221=1⋅689−2⋅234234=1⋅221+13⇒13=1⋅234−1⋅221221=17⋅13+0so gcd⁡(689,234)=13.\begin{aligned} 689 &= 2 \cdot 234 + 221 &\Rightarrow\quad 221 &= 1\cdot 689 - 2\cdot 234 \\ 234 &= 1 \cdot 221 + 13 &\Rightarrow\quad 13 &= 1\cdot 234 - 1\cdot 221 \\ 221 &= 17 \cdot 13 + 0 &&\text{so } \gcd(689,234) = 13. \end{aligned}

Back-substitute, eliminating the remainder 221221:

13=1⋅234−1⋅221=1⋅234−1 (1⋅689−2⋅234)=3⋅234−1⋅689.\begin{aligned} 13 &= 1\cdot 234 - 1\cdot 221 \\ &= 1\cdot 234 - 1\,(1\cdot 689 - 2\cdot 234) \\ &= 3\cdot 234 - 1\cdot 689. \end{aligned}

So 689(−1)+234(3)=13689(-1) + 234(3) = 13, giving x=−1x = -1, y=3y = 3.

Summary