Skip to content

Divisibility and the Euclidean Algorithm

Every HTTPS connection you make begins with a division step. When your browser verifies a certificate, it computes gcd⁡(a,b)\gcd(a, b) for 600-digit numbers using the same algorithm Euclid wrote down around 300 BCE. When a computer algebra system simplifies a fraction, it uses the same algorithm. When a number theorist proves that 2\sqrt{2} is irrational, the proof goes through the same lemma.

The reason this one idea recurs across 2,300 years of mathematics, from geometry to public-key cryptography, is that the Euclidean algorithm is not just a method for computing a greatest common divisor. It is the theorem that reveals the structure of the integers: that every ideal is principal, that primes are irreducible, that unique factorisation holds. These are not obvious properties of Z\mathbb{Z}; they fail in other rings; and the proof that they hold goes through Bézout’s identity, which goes through the Euclidean algorithm.

This page builds that chain from the ground up, the way Aluffi builds ring theory: by constructing the tools you’ll need later and understanding why they work, not just verifying that they do.

Here is a concrete question that has nothing to do with number theory at first glance.

You have two audio files of lengths 252 and 198 seconds. You want to cut both into identical segments of maximum length so that nothing is wasted. How long should each segment be?

If you think about this for a moment, you realise you need the largest integer that divides both 252 and 198. That is gcd⁡(252,198)\gcd(252, 198). But how do you find it? You could try every integer from 198 down to 1; but for 600-digit numbers, that’s not just slow, it’s longer than the age of the universe.

The answer; the idea that makes the computation feasible; is the Euclidean algorithm. And the reason the Euclidean algorithm works is a property of the integers that most people never think to question: the division algorithm. This page builds from the division algorithm to Bézout’s identity to the extended Euclidean algorithm, and each step earns the next.

Exercise 0.1. Before reading further, try to find gcd⁡(252,198)\gcd(252, 198) by listing all divisors of each number. Then notice how much faster the method on this page will be.

The integers are the first number system where a question like “does aa divide bb?” is interesting. In Q\mathbb{Q} or R\mathbb{R}, every nonzero number divides every other number (b=a⋅bab = a \cdot \frac{b}{a}), so divisibility is trivial. The integers are special because division is restricted: the quotient must also be an integer.

This restriction is what creates structure; and what makes number theory rich.

Definition. For integers aa and bb with a≠0a \neq 0, we say aa divides bb, written a∣ba \mid b, if there exists an integer kk such that b=akb = ak.

Exercise 0.2. Show that a∣0a \mid 0 for every a≠0a \neq 0, and that 0∣b0 \mid b only for b=0b = 0.

The definition is simple, but the consequences are not. The most important one:

Proposition (Linearity of Divisibility). If a∣ba \mid b and a∣ca \mid c, then a∣(bx+cy)a \mid (bx + cy) for all integers x,yx, y.

Proof. b=amb = am and c=anc = an for some integers m,nm, n. Then bx+cy=a(mx+ny)bx + cy = a(mx + ny), so aa divides the result. ■\blacksquare

This “linearity” is what makes divisibility proofs work: to show n∣f(n)−g(n)n \mid f(n) - g(n), show nn divides each term separately.

Exercise 0.3. Show that if n∣a−bn \mid a - b and n∣c−dn \mid c - d, then n∣ac−bdn \mid ac - bd. Hint: add and subtract bcbc.

This exercise is not busywork; the result is exactly what makes modular arithmetic well-defined, and it is the proof you’ll need in Congruences.

0.3 The Division Algorithm: More Than Long Division

Section titled “0.3 The Division Algorithm: More Than Long Division”

Every schoolchild knows how to divide 252 by 198: the quotient is 1 and the remainder is 54. What most never question is that the answer is unique; that you couldn’t also get quotient 2 with remainder −144. The uniqueness is what makes the algorithm deterministic and the concept of “remainder” well-defined.

Theorem (Division Algorithm). For integers aa and bb with b>0b > 0, there exist unique integers qq (quotient) and rr (remainder) such that

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

Proof of existence. The set {bq≤a:q∈Z}\{bq \leq a : q \in \mathbb{Z}\} is non-empty (take qq sufficiently negative) and bounded above by a/ba/b. Let qq be its supremum, which is an integer by the Archimedean property. Set r=a−bq≥0r = a - bq \geq 0. If r≥br \geq b, then a−b(q+1)≥0a - b(q + 1) \geq 0, contradicting the maximality of qq. ■\blacksquare

Proof of uniqueness. Suppose a=bq1+r1=bq2+r2a = bq_1 + r_1 = bq_2 + r_2 with both remainders in [0,b)[0, b). Then b(q1−q2)=r2−r1b(q_1 - q_2) = r_2 - r_1. Since ∣r2−r1∣<b|r_2 - r_1| < b, we need q1=q2q_1 = q_2 and r2=r1r_2 = r_1. ■\blacksquare

The uniqueness proof is worth understanding deeply. It works because the interval [0,b)[0, b) is strictly smaller than the spacing between consecutive multiples of bb. If you widened the interval to [0,b][0, b], two quotients could work. This is not pedantry; in Z[i]\mathbb{Z}[i] (Gaussian integers), the “remainder” is measured by a norm, and the norm function does not always decrease the way Z\mathbb{Z}‘s does. When it does decrease, the ring has unique factorisation (like Z[i]\mathbb{Z}[i]); when it doesn’t, it doesn’t (like Z[−5]\mathbb{Z}[\sqrt{-5}]). The division algorithm is not just a computational tool; it is the property that makes the entire edifice of arithmetic stand.

Exercise 0.4. Prove the division algorithm for b<0b < 0 by reducing to the case b>0b > 0. What changes in the inequality 0≤r<∣b∣0 \leq r < |b|?

Exercise 0.5. Show that for any integer aa, a2−aa^2 - a is divisible by 3. Hint: a≡0,1, or 2(mod3)a \equiv 0, 1, \text{ or } 2 \pmod 3; but you can also prove it by factoring a2−a=a(a−1)a^2 - a = a(a - 1) and checking two consecutive integers.

0.4 The Greatest Common Divisor: Definition and Existence

Section titled “0.4 The Greatest Common Divisor: Definition and Existence”

Definition. For integers a,ba, b not both zero, gcd⁡(a,b)\gcd(a, b) is the largest positive integer dividing both.

Why this definition works. You might worry: how do we know there’s a largest common divisor? The set of common divisors is finite (all bounded by max⁡(∣a∣,∣b∣)\max(|a|, |b|)), so a maximum exists by the well-ordering of Z≥0\mathbb{Z}_{\geq 0}. This is one of those cases where the structure of Z≥0\mathbb{Z}_{\geq 0} (every non-empty subset has a least element) does the heavy lifting.

Theorem (Bézout’s Identity). gcd⁡(a,b)\gcd(a, b) is the smallest positive integer in the set

S={ax+by:x,y∈Z}.S = \{ax + by : x, y \in \mathbb{Z}\}.

In particular, there exist integers x,yx, y with ax+by=gcd⁡(a,b)ax + by = \gcd(a, b).

This is the first genuinely structural theorem in number theory, and the proof technique (well-ordering of a non-empty set of positive integers) is the single most important proof method in the subject. Let’s look at it carefully.

Proof. SS is non-empty (if a≠0a \neq 0, take x=sign⁡(a)x = \operatorname{sign}(a), y=0y = 0). By the well-ordering principle, SS has a least element d=ax0+by0>0d = ax_0 + by_0 > 0. We show d=gcd⁡(a,b)d = \gcd(a, b) in two steps.

Step 1: d∣ad \mid a and d∣bd \mid b. Divide aa by dd: a=dq+ra = dq + r with 0≤r<d0 \leq r < d. Then

r=a−qd=a−q(ax0+by0)=a(1−qx0)+b(−qy0)∈S.r = a - qd = a - q(ax_0 + by_0) = a(1 - qx_0) + b(-qy_0) \in S.

But 0≤r<d0 \leq r < d contradicts the minimality of dd unless r=0r = 0. So d∣ad \mid a. Similarly d∣bd \mid b.

Step 2: dd is the greatest common divisor. If c∣ac \mid a and c∣bc \mid b, then c∣dc \mid d (because d=ax0+by0d = ax_0 + by_0 is a linear combination). So c≤dc \leq d. ■\blacksquare

Why this proof matters. Look at Step 1 again. The key move is that the remainder rr is itself an element of SS; because SS is closed under subtraction of elements of the form ax+byax + by. This is exactly the argument that shows every ideal of Z\mathbb{Z} is principal, and it is the proof template for Bézout domains in ring theory. The Euclidean algorithm doesn’t just compute a gcd; it proves a structural theorem about the integers.

Exercise 0.6. Find gcd⁡(12,42)\gcd(12, 42) and Bézout coefficients. Then find all solutions to 12x+42y=612x + 42y = 6. Answer: gcd⁡=6\gcd = 6; 12(−3)+42(1)=612(-3) + 42(1) = 6; all solutions are x=−3+7tx = -3 + 7t, y=1−2ty = 1 - 2t.

Exercise 0.7. Show that gcd⁡(a,b)=gcd⁡(a,b−a)=gcd⁡(a,a+b)\gcd(a, b) = \gcd(a, b - a) = \gcd(a, a + b). Why this matters: this is the mathematical basis of the subtraction-based Euclidean algorithm that Euclid actually wrote down (Elements, Book VII, Propositions 1–2).

A consequence you’ll need later. gcd⁡(a,b)=1\gcd(a, b) = 1 (coprimality) is equivalent to the existence of x,yx, y with ax+by=1ax + by = 1. This is the definition of a unimodular row, and it is the reason modular inverses exist (when gcd⁡(a,n)=1\gcd(a, n) = 1, the inverse of aa mod nn is the Bézout coefficient xx). See Congruences.

Euclid’s lemma falls out immediately: if a∣bca \mid bc and gcd⁡(a,b)=1\gcd(a, b) = 1, write 1=ax+by1 = ax + by, multiply by cc: c=acx+bcyc = acx + bcy. Since a∣acxa \mid acx and a∣bcya \mid bcy (because a∣bca \mid bc), we get a∣ca \mid c. This is the key step in proving unique factorisation; and it fails in Z[−5]\mathbb{Z}[\sqrt{-5}] precisely because Bézout fails there, which is why unique factorisation fails there.

The division algorithm says: a=bq+ra = bq + r. The crucial observation is that

gcd⁡(a,b)=gcd⁡(b,r).\gcd(a, b) = \gcd(b, r).

Why? Any common divisor of aa and bb divides r=a−bqr = a - bq (by linearity of divisibility). Any common divisor of bb and rr divides a=bq+ra = bq + r (same reason). So the two pairs have exactly the same set of common divisors, hence the same greatest common divisor.

This means we can replace the pair (a,b)(a, b) by the smaller pair (b,r)(b, r) with the same gcd - and iterate:

(a,b)→(b,r1)→(r1,r2)→(r2,r3)→⋯(a, b) \to (b, r_1) \to (r_1, r_2) \to (r_2, r_3) \to \cdots

The remainders satisfy r1>r2>r3>⋯≥0r_1 > r_2 > r_3 > \cdots \geq 0, so the sequence must terminate. When it terminates, the last non-zero remainder is gcd⁡(a,b)\gcd(a, b).

Worked example. gcd⁡(252,198)\gcd(252, 198):

StepEquationObservation
1252=198⋅1+54252 = 198 \cdot 1 + 54gcd⁡(252,198)=gcd⁡(198,54)\gcd(252, 198) = \gcd(198, 54)
2198=54⋅3+36198 = 54 \cdot 3 + 36gcd⁡(198,54)=gcd⁡(54,36)\gcd(198, 54) = \gcd(54, 36)
354=36⋅1+1854 = 36 \cdot 1 + 18gcd⁡(54,36)=gcd⁡(36,18)\gcd(54, 36) = \gcd(36, 18)
436=18⋅2+036 = 18 \cdot 2 + 0gcd⁡(36,18)=gcd⁡(18,0)=18\gcd(36, 18) = \gcd(18, 0) = 18

Answer: gcd⁡(252,198)=18\gcd(252, 198) = 18.

Back-substitute through the table to express the gcd as a linear combination:

18=36−18⋅1=36−(54−36⋅1)⋅1=36⋅2−54=(198−54⋅3)⋅2−54=198⋅2−54⋅7=198⋅2−(252−198)⋅7=252⋅(−7)+198⋅9.\begin{aligned} 18 &= 36 - 18 \cdot 1 \\ &= 36 - (54 - 36 \cdot 1) \cdot 1 = 36 \cdot 2 - 54 \\ &= (198 - 54 \cdot 3) \cdot 2 - 54 = 198 \cdot 2 - 54 \cdot 7 \\ &= 198 \cdot 2 - (252 - 198) \cdot 7 = 252 \cdot (-7) + 198 \cdot 9. \end{aligned}

So 252(−7)+198(9)=18252(-7) + 198(9) = 18: Bézout coefficients x=−7x = -7, y=9y = 9.

This back-substitution is mechanical and tedious. The extended Euclidean algorithm automates it by maintaining the coefficients alongside the remainders; and this is what you actually implement when computing modular inverses for RSA decryption.

The connection to cryptography. To find the decryption exponent dd in RSA, you compute the inverse of ee modulo φ(n)\varphi(n); which is exactly the extended Euclidean algorithm applied to (e,φ(n))(e, \varphi(n)). The 2,300-year-old algorithm is running in every TLS handshake.

PitfallWhy it happensFix
Negative remainder from division algorithmAdjusting qq the wrong way for negative aaUse r=((a mod b)+b) mod br = ((a \bmod b) + b) \bmod b
gcd⁡(0,0)\gcd(0, 0)No largest common divisor existsDefine gcd⁡(0,0)=0\gcd(0,0) = 0 by convention, or don’t call the function
Bézout coefficients not unique(x+tb,y−ta)(x + tb, y - ta) also works for all t∈Zt \in \mathbb{Z}Report one pair; the exam wants a pair
Cancelling cc from ac≡bcac \equiv bcgcd⁡(c,n)≠1\gcd(c, n) \neq 1Only cancel when cc is invertible mod nn
Division Algorithm⟹Euclidean Algorithm terminates⟹gcd⁡(a,b)=gcd⁡(b,a mod b)⟹Beˊzout: gcd⁡(a,b)=ax+by⟹Euclid’s lemma: a∣bc,gcd⁡(a,b)=1⇒a∣c⟹Unique factorisation (next page)\begin{array}{ccc} \text{Division Algorithm} & \Longrightarrow & \text{Euclidean Algorithm terminates} \\ & \Longrightarrow & \gcd(a,b) = \gcd(b, a \bmod b) \\ & \Longrightarrow & \text{Bézout: } \gcd(a,b) = ax + by \\ & \Longrightarrow & \text{Euclid's lemma: } a \mid bc, \gcd(a,b)=1 \Rightarrow a \mid c \\ & \Longrightarrow & \text{Unique factorisation (next page)} \end{array}

Each step uses the previous one. The division algorithm is the foundation; unique factorisation is the skyscraper. The next page builds the tower.