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 dad \mid a and dbd \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 aba \mid b

If a0a \neq 0 and aba \mid b, then gcd(a,b)=a\gcd(a, b) = |a|.

Adding Multiples

For any integers k,k, \ell:

gcd(a,b)=gcd(akb,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,amodb)\gcd(a, b) = \gcd(b, a \bmod b)

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

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

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

Thus {d:dadb}={d:dbdr}\{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,amodb)\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: amodb<ba \bmod b < b.

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

Example

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

Stepaabbamodba \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=218+1248 = 2 \cdot 18 + 12
  • 18=112+618 = 1 \cdot 12 + 6
  • 12=26+012 = 2 \cdot 6 + 0

Back-substituting:

  • 6=181126 = 18 - 1 \cdot 12
  • 6=181(48218)=3181486 = 18 - 1 \cdot (48 - 2 \cdot 18) = 3 \cdot 18 - 1 \cdot 48

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

Euclid’s Lemma

Statement

Lemma (Euclid’s Lemma): If pp is prime and pabp \mid ab, then pap \mid a or pbp \mid b.

Proof

Suppose pap \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 pabp \mid ab, we have pabyp \mid aby. Clearly ppxbp \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 d1bd_1 \mid b, d2cd_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 d1bd_1 \mid b and d2cd_2 \mid c), so d1d2d_1 d_2 divides bcbc.

Thus d1d2gcd(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,amodb)\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 pabp \mid ab, then pap \mid a or pbp \mid b