Skip to content
Part IA Michaelmas, Lent Term

Greatest Common Divisor and Euclid's Algorithm

Greatest Common Divisor

Definition

For integers aa and bb (not both zero), the greatest common divisor gcd⁡(a,b)\gcd(a, b) is the largest positive integer dd such that d∣ad \mid a and d∣bd \mid b.

By convention, gcd⁡(0,0)=0\gcd(0, 0) = 0.

Examples

  • gcd⁡(12,18)=6\gcd(12, 18) = 6
  • gcd⁡(17,5)=1\gcd(17, 5) = 1
  • gcd⁡(100,0)=100\gcd(100, 0) = 100

Coprimality

We say aa and bb are coprime or relatively prime if gcd⁡(a,b)=1\gcd(a, b) = 1.

Properties of GCD

Symmetry

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

Absolute Value

gcd⁡(a,b)=gcd⁡(∣a∣,∣b∣)\gcd(a, b) = \gcd(|a|, |b|)

If a∣ba \mid b

If a≠0a \neq 0 and a∣ba \mid b, then gcd⁡(a,b)=∣a∣\gcd(a, b) = |a|.

Adding Multiples

For any integers k,ℓk, \ell:

gcd⁡(a,b)=gcd⁡(a−kb,b)\gcd(a, b) = \gcd(a - kb, b)

This is the key insight behind Euclid’s algorithm.

Euclid’s Algorithm

The Key Lemma

gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b)

Proof: Let a=bq+ra = bq + r with 0≤r<b0 \leq r < b.

Any common divisor of aa and bb divides r=a−bqr = a - bq.

Any common divisor of bb and rr divides a=bq+ra = bq + r.

Thus {d:d∣a∧d∣b}={d:d∣b∧d∣r}\{d : d \mid a \land d \mid b\} = \{d : d \mid b \land d \mid r\}.

Taking the maximum, gcd⁡(a,b)=gcd⁡(b,r)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, r) = \gcd(b, a \bmod b).

The Algorithm

Euclid’s algorithm computes gcd⁡(a,b)\gcd(a, b) recursively:

function gcd(a, b):
    if b = 0:
        return a
    else:
        return gcd(b, a mod b)

Termination

The second argument strictly decreases at each step: a mod b<ba \bmod b < b.

Since b≥0b \geq 0 and the non-negative integers are well-ordered, the algorithm terminates.

Example

Compute gcd⁡(48,18)\gcd(48, 18):

Stepaabba mod ba \bmod b
1481812
218126
31260
460—

gcd⁡(48,18)=6\gcd(48, 18) = 6.

Bézout’s Identity

Statement

Theorem (Bézout’s Identity): For integers aa and bb (not both zero), there exist integers xx and yy such that:

ax+by=gcd⁡(a,b)ax + by = \gcd(a, b)

Extended Euclidean Algorithm

We compute xx and yy alongside the GCD by tracking linear combinations.

Example: Find integers x,yx, y with 48x+18y=gcd⁡(48,18)=648x + 18y = \gcd(48, 18) = 6.

Working through Euclid’s algorithm:

  • 48=2⋅18+1248 = 2 \cdot 18 + 12
  • 18=1⋅12+618 = 1 \cdot 12 + 6
  • 12=2⋅6+012 = 2 \cdot 6 + 0

Back-substituting:

  • 6=18−1⋅126 = 18 - 1 \cdot 12
  • 6=18−1⋅(48−2⋅18)=3⋅18−1⋅486 = 18 - 1 \cdot (48 - 2 \cdot 18) = 3 \cdot 18 - 1 \cdot 48

So x=−1x = -1 and y=3y = 3 verify 48⋅(−1)+18⋅3=−48+54=648 \cdot (-1) + 18 \cdot 3 = -48 + 54 = 6.

Euclid’s Lemma

Statement

Lemma (Euclid’s Lemma): If pp is prime and p∣abp \mid ab, then p∣ap \mid a or p∣bp \mid b.

Proof

Suppose p∤ap \nmid a. Since pp is prime, gcd⁡(p,a)=1\gcd(p, a) = 1.

By Bézout’s Identity, there exist x,yx, y with px+ay=1px + ay = 1.

Multiply by bb: pxb+aby=bpxb + aby = b.

Since p∣abp \mid ab, we have p∣abyp \mid aby. Clearly p∣pxbp \mid pxb.

Thus p∣(pxb+aby)=bp \mid (pxb + aby) = b.

Multiplicativity of GCD

Statement

If gcd⁡(b,c)=1\gcd(b, c) = 1, then:

gcd⁡(a,bc)=gcd⁡(a,b)⋅gcd⁡(a,c)\gcd(a, bc) = \gcd(a, b) \cdot \gcd(a, c)

Proof Sketch

Let d1=gcd⁡(a,b)d_1 = \gcd(a, b) and d2=gcd⁡(a,c)d_2 = \gcd(a, c).

Since gcd⁡(b,c)=1\gcd(b, c) = 1 and d1∣bd_1 \mid b, d2∣cd_2 \mid c, we have gcd⁡(d1,d2)=1\gcd(d_1, d_2) = 1.

Both d1d_1 and d2d_2 divide aa, so d1d2d_1 d_2 divides aa.

Both d1d_1 and d2d_2 divide bcbc (since d1∣bd_1 \mid b and d2∣cd_2 \mid c), so d1d2d_1 d_2 divides bcbc.

Thus d1d2∣gcd⁡(a,bc)d_1 d_2 \mid \gcd(a, bc).

For the reverse, use Bézout’s identity with the coprimality assumptions.


Summary

  • gcd⁡(a,b)\gcd(a, b) is the largest common divisor, defined for a,ba, b not both zero
  • Key lemma: gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b)
  • Euclid’s algorithm terminates by well-foundedness
  • Bézout’s Identity: ax+by=gcd⁡(a,b)ax + by = \gcd(a, b) for some x,yx, y
  • Euclid’s Lemma: if prime p∣abp \mid ab, then p∣ap \mid a or p∣bp \mid b