evan's notes
cal 3discreteodeenv systems
中文

On this page

  • Greatest common divisors
  • Why listing divisors does not scale
  • The division algorithm
  • Existence
  • Negative-dividend example
  • Uniqueness proof
  • The gcd reduction theorem
  • Proof by matching common-divisor sets
  • Euclid's algorithm
  • Why it terminates
  • Example: gcd⁡(35,21)\gcd(35,21)gcd(35,21)
  • Bézout's identity
  • Why back-substitution works
  • Extended example: gcd⁡(515,42)\gcd(515,42)gcd(515,42)

Discrete Structures

§11 Greatest Common Divisors and Euclid's Algorithm

Evan Luo · Oct 20, 2026

Discrete Structures

§11 Greatest Common Divisors and Euclid's Algorithm

Evan Luo2 days ago

7 min read

The greatest common divisor can be found without listing every divisor. The key is to replace a pair of integers by a smaller pair with exactly the same common divisors, then repeat.

Greatest common divisors

Let a,b∈Za,b\in\mathbb Za,b∈Z and suppose they are not both zero. Their greatest common divisor, written gcd⁡(a,b)\gcd(a,b)gcd(a,b), is the greatest positive integer that divides both aaa and bbb. Equivalently, d=gcd⁡(a,b)d=\gcd(a,b)d=gcd(a,b) means

d>0,d∣a,d∣b,c∣a and c∣b⟹∣c∣≤d.\boxed{ d>0,\qquad d\mid a,\qquad d\mid b, \qquad c\mid a\text{ and }c\mid b\Longrightarrow |c|\le d. } d>0,d∣a,d∣b,c∣a and c∣b⟹∣c∣≤d.​

The absolute value in the last condition accounts for negative common divisors. In particular, for a≠0a\ne0a=0,

gcd⁡(a,0)=∣a∣.\gcd(a,0)=|a|. gcd(a,0)=∣a∣.

The pair (0,0)(0,0)(0,0) is excluded because every nonzero integer divides both entries, so there is no greatest positive common divisor.

For example, the positive common divisors of 161616 and 242424 are 1,2,4,81,2,4,81,2,4,8, so

gcd⁡(16,24)=8.\gcd(16,24)=8. gcd(16,24)=8.

Why listing divisors does not scale

For a positive integer nnn, a direct method tests possible divisors up to n\sqrt nn​. A divisor found below that point also reveals its complementary divisor n/dn/dn/d, so the method still recovers divisors larger than n\sqrt nn​. If nnn has binary input length LLL, then L≈log⁡2nL\approx\log_2 nL≈log2​n, while the number of trial divisions is roughly

n=212log⁡2n≈(2)L.\sqrt n=2^{\frac12\log_2 n}\approx(\sqrt2)^L. n​=221​log2​n≈(2​)L.

That is exponential in the number of input bits. Euclid's algorithm avoids this divisor search.

The division algorithm

For every a,b∈Za,b\in\mathbb Za,b∈Z with b≠0b\ne0b=0, there are unique integers qqq and rrr such that

a=qb+r,0≤r<∣b∣.\boxed{a=qb+r, \qquad 0\le r<|b|.} a=qb+r,0≤r<∣b∣.​

The integer qqq is the quotient, and rrr is the remainder. The remainder condition is part of the theorem; without it, many different pairs would satisfy a=qb+ra=qb+ra=qb+r.

Existence

First suppose b>0b>0b>0. If a≥ba\ge ba≥b, long division—or repeated subtraction of bbb—produces a quotient and a remainder in the required range. If a<ba<ba<b, choose a nonnegative integer kkk large enough that a+kb≥ba+kb\ge ba+kb≥b. Apply the first case to a+kba+kba+kb:

a+kb=q′b+r,0≤r<b.a+kb=q'b+r, \qquad 0\le r<b. a+kb=q′b+r,0≤r<b.

Subtracting kbkbkb gives a=(q′−k)b+ra=(q'-k)b+ra=(q′−k)b+r. If b<0b<0b<0, apply the positive-divisor case to −b-b−b and rewrite q′(−b)q'(-b)q′(−b) as (−q′)b(-q')b(−q′)b. These cases cover every nonzero integer bbb.

Negative-dividend example

The quotient is allowed to be negative. For a=−5a=-5a=−5 and b=3b=3b=3,

−5=3(−2)+1,0≤1<3.-5=3(-2)+1, \qquad 0\le1<3. −5=3(−2)+1,0≤1<3.

Thus the quotient is −2-2−2 and the remainder is 111.

Uniqueness proof

Suppose two quotient–remainder pairs both satisfy the theorem:

a=q1b+r1,0≤r1<∣b∣,a=q2b+r2,0≤r2<∣b∣.\begin{aligned} a&=q_1b+r_1, & 0&\le r_1<|b|,\\ a&=q_2b+r_2, & 0&\le r_2<|b|. \end{aligned} aa​=q1​b+r1​,=q2​b+r2​,​00​≤r1​<∣b∣,≤r2​<∣b∣.​

Subtract the two equations:

0=(q1−q2)b+(r1−r2),r2−r1=(q1−q2)b.0=(q_1-q_2)b+(r_1-r_2), \qquad r_2-r_1=(q_1-q_2)b. 0=(q1​−q2​)b+(r1​−r2​),r2​−r1​=(q1​−q2​)b.

Therefore b∣(r2−r1)b\mid(r_2-r_1)b∣(r2​−r1​). The remainder bounds also give

−∣b∣<r2−r1<∣b∣.-|b|<r_2-r_1<|b|. −∣b∣<r2​−r1​<∣b∣.

The only multiple of bbb strictly between −∣b∣-|b|−∣b∣ and ∣b∣|b|∣b∣ is 000. Hence r2−r1=0r_2-r_1=0r2​−r1​=0, so r1=r2r_1=r_2r1​=r2​. Returning to the subtracted equation and using b≠0b\ne0b=0 then gives

(q1−q2)b=0⟹q1=q2.(q_1-q_2)b=0 \quad\Longrightarrow\quad q_1=q_2. (q1​−q2​)b=0⟹q1​=q2​.

Both the quotient and the remainder are unique.

The gcd reduction theorem

Let a,b,q,r∈Za,b,q,r\in\mathbb Za,b,q,r∈Z, with (a,b)≠(0,0)(a,b)\ne(0,0)(a,b)=(0,0). If

a=qb+r,a=qb+r, a=qb+r,

then

gcd⁡(a,b)=gcd⁡(b,r).\boxed{\gcd(a,b)=\gcd(b,r).} gcd(a,b)=gcd(b,r).​

The theorem does more than preserve the value of the gcd: the two pairs have exactly the same common divisors.

Proof by matching common-divisor sets

Let

C(u,v)={c∈Z∣c∣u and c∣v}.C(u,v)=\{c\in\mathbb Z\mid c\mid u\text{ and }c\mid v\}. C(u,v)={c∈Z∣c∣u and c∣v}.

We prove both inclusions.

Take c∈C(a,b)c\in C(a,b)c∈C(a,b). Then c∣ac\mid ac∣a and c∣bc\mid bc∣b. Since a divisor of bbb also divides qbqbqb, the difference property gives

c∣(a−qb)=r.c\mid(a-qb)=r. c∣(a−qb)=r.

Thus ccc divides both bbb and rrr, so c∈C(b,r)c\in C(b,r)c∈C(b,r). Therefore C(a,b)⊆C(b,r)C(a,b)\subseteq C(b,r)C(a,b)⊆C(b,r).

Conversely, take c∈C(b,r)c\in C(b,r)c∈C(b,r). Then c∣bc\mid bc∣b and c∣rc\mid rc∣r, so ccc divides their integer linear combination

qb+r=a.qb+r=a. qb+r=a.

Thus ccc divides both aaa and bbb, so c∈C(a,b)c\in C(a,b)c∈C(a,b). Therefore C(b,r)⊆C(a,b)C(b,r)\subseteq C(a,b)C(b,r)⊆C(a,b).

The two inclusions show

C(a,b)=C(b,r),C(a,b)=C(b,r), C(a,b)=C(b,r),

so the greatest positive element of each set is the same. Hence gcd⁡(a,b)=gcd⁡(b,r)\gcd(a,b)=\gcd(b,r)gcd(a,b)=gcd(b,r).

Euclid's algorithm

For positive inputs, set r−2=ar_{-2}=ar−2​=a and r−1=br_{-1}=br−1​=b, then repeatedly apply the division algorithm:

a=q0b+r0,0≤r0<b,b=q1r0+r1,0≤r1<r0,r0=q2r1+r2,0≤r2<r1,⋮\begin{aligned} a&=q_0b+r_0, & 0&\le r_0<b,\\ b&=q_1r_0+r_1, & 0&\le r_1<r_0,\\ r_0&=q_2r_1+r_2, & 0&\le r_2<r_1,\\ &\hspace{2.5em}\vdots \end{aligned} abr0​​=q0​b+r0​,=q1​r0​+r1​,=q2​r1​+r2​,⋮​000​≤r0​<b,≤r1​<r0​,≤r2​<r1​,​

Each step preserves the gcd. Stop at the first index mmm for which rm=0r_m=0rm​=0, then return the positive divisor rm−1r_{m-1}rm−1​. If the first division already has remainder zero, this returns r−1=br_{-1}=br−1​=b.

Why it terminates

Every nonzero remainder is a nonnegative integer strictly smaller than the preceding positive divisor. If the first remainder is not already zero, the positive remainders form a strictly decreasing sequence. Such a sequence cannot continue forever, so a zero remainder must eventually appear.

If rm=0r_m=0rm​=0 is the first zero remainder, then the final division has positive divisor rm−1r_{m-1}rm−1​. Repeated use of the reduction theorem gives

gcd⁡(a,b)=gcd⁡(r−2,r−1)=⋯=gcd⁡(rm−2,rm−1)=rm−1.\gcd(a,b)=\gcd(r_{-2},r_{-1})=\cdots=\gcd(r_{m-2},r_{m-1})=r_{m-1}. gcd(a,b)=gcd(r−2​,r−1​)=⋯=gcd(rm−2​,rm−1​)=rm−1​.

Example: gcd⁡(35,21)\gcd(35,21)gcd(35,21)

35=1(21)+14,21=1(14)+7,14=2(7)+0.\begin{aligned} 35&=1(21)+14,\\ 21&=1(14)+7,\\ 14&=2(7)+0. \end{aligned} 352114​=1(21)+14,=1(14)+7,=2(7)+0.​

The last nonzero remainder is 777, so

gcd⁡(35,21)=7.\gcd(35,21)=7. gcd(35,21)=7.

Bézout's identity

Let a,b∈Za,b\in\mathbb Za,b∈Z be not both zero, and let d=gcd⁡(a,b)d=\gcd(a,b)d=gcd(a,b). Bézout's identity says that there are integers sss and ttt such that

d=sa+tb.\boxed{d=sa+tb.} d=sa+tb.​

The numbers sss and ttt are called Bézout coefficients. They need not be positive or unique.

Why back-substitution works

Each Euclidean step can be rearranged as

ri=ri−2−qiri−1.r_i=r_{i-2}-q_i r_{i-1}. ri​=ri−2​−qi​ri−1​.

The starting values r−2=ar_{-2}=ar−2​=a and r−1=br_{-1}=br−1​=b are already integer linear combinations of aaa and bbb. The displayed equation shows inductively that every later remainder is also an integer linear combination of aaa and bbb. Since the divisor immediately before the first zero remainder equals ddd, reversing the Euclidean steps expresses ddd as sa+tbsa+tbsa+tb.

The zero-input cases are direct. If b=0b=0b=0, then d=∣a∣d=|a|d=∣a∣: choose s=1,t=0s=1,t=0s=1,t=0 when a>0a>0a>0, and s=−1,t=0s=-1,t=0s=−1,t=0 when a<0a<0a<0. If a=0a=0a=0, choose s=0,t=1s=0,t=1s=0,t=1 when b>0b>0b>0, and s=0,t=−1s=0,t=-1s=0,t=−1 when b<0b<0b<0. When both inputs are nonzero, apply the positive-input argument to ∣a∣|a|∣a∣ and ∣b∣|b|∣b∣, then change the corresponding coefficient signs when an original input is negative. This completes the proof for all integer inputs that are not both zero.

For the smaller example above,

7=21−14=21−(35−21)=−35+2(21).7=21-14=21-(35-21)=-35+2(21). 7=21−14=21−(35−21)=−35+2(21).

Thus one pair of Bézout coefficients for 353535 and 212121 is s=−1s=-1s=−1, t=2t=2t=2.

Extended example: gcd⁡(515,42)\gcd(515,42)gcd(515,42)

Run Euclid's algorithm:

515=12(42)+11,42=3(11)+9,11=1(9)+2,9=4(2)+1,2=2(1)+0.\begin{aligned} 515&=12(42)+11,\\ 42&=3(11)+9,\\ 11&=1(9)+2,\\ 9&=4(2)+1,\\ 2&=2(1)+0. \end{aligned} 515421192​=12(42)+11,=3(11)+9,=1(9)+2,=4(2)+1,=2(1)+0.​

The last nonzero remainder is 111, so gcd⁡(515,42)=1\gcd(515,42)=1gcd(515,42)=1. Now substitute backward:

1=9−4(2)=9−4(11−9)=5(9)−4(11)=5(42−3(11))−4(11)=5(42)−19(11)=5(42)−19(515−12(42))=233(42)−19(515).\begin{aligned} 1 &=9-4(2)\\ &=9-4(11-9)\\ &=5(9)-4(11)\\ &=5(42-3(11))-4(11)\\ &=5(42)-19(11)\\ &=5(42)-19(515-12(42))\\ &=233(42)-19(515). \end{aligned} 1​=9−4(2)=9−4(11−9)=5(9)−4(11)=5(42−3(11))−4(11)=5(42)−19(11)=5(42)−19(515−12(42))=233(42)−19(515).​

Therefore,

1=−19(515)+233(42).\boxed{1=-19(515)+233(42).} 1=−19(515)+233(42).​

This simultaneously verifies the gcd and gives Bézout coefficients s=−19s=-19s=−19 and t=233t=233t=233.

Source: https://notes.ohevan.com/notes/discrete-structures/11-greatest-common-divisors-and-euclids-algorithm

© 2026 Evan Luo. All rights reserved.

Back to Discrete Structures

0 reads

·Last edited Today
  • newsletter

  • about me

  • sponsor

© 2026 Evan Luo. All rights reserved.