GCD and the Euclidean Algorithm

Introduction

The greatest common divisor is one of the oldest and most fundamental concepts in mathematics. When we reduce a fraction to lowest terms, we divide both numerator and denominator by their greatest common divisor. When we solve Diophantine equations, we need to know whether a solution exists—and the GCD tells us. When we implement the RSA cryptosystem, the Euclidean algorithm computes the modular inverses that make encryption possible.

The Euclidean algorithm, described by Euclid around 300 300 BCE, is arguably the oldest non-trivial algorithm still in widespread use today. Its elegance lies in reducing a seemingly difficult problem—finding the largest common factor of two numbers—to a sequence of simple divisions. The algorithm is fast, easy to implement, and reveals deep structural properties of the integers.

Understanding the GCD and Euclidean algorithm is essential for number theory, abstract algebra, cryptography, and computer science. These concepts form the foundation upon which modular arithmetic, prime factorization theory, and much of modern cryptography are built.

Definition of the Greatest Common Divisor

Basic Definition

Let a a and b b be integers, not both zero. A common divisor of a a and b b is an integer d d such that d⁢a∣ d a vertical bar and d⁢b∣ d b vertical bar (that is, d d divides both a a and b b).

The greatest common divisor of a a and b b, denoted gcd⁡(a,b) the gcd of open paren a comma b close paren or simply (a,b) open paren a comma b close paren, is the largest positive integer that divides both a a and b b.

gcd⁡(a,b)=max⁡(d∈ℤ+:d) the gcd of open paren a comma b close paren equals max of open paren d is a member of the positive integers colon d close paren

By convention, gcd⁡(a,0)=|a| the gcd of open paren a comma 0 close paren equals the absolute value of a for any nonzero integer a a, since every integer divides 0 0. The expression gcd⁡(0,0) the gcd of open paren 0 comma 0 close paren is typically left undefined or set to 0 0 by convention.

Examples

The divisors of 12 12 are {1,2,3,4,6,12} the set 1 comma 2 comma 3 comma 4 comma 6 comma 12. The divisors of 18 18 are {1,2,3,6,9,18} the set 1 comma 2 comma 3 comma 6 comma 9 comma 18. The common divisors are {1,2,3,6} the set 1 comma 2 comma 3 comma 6, so gcd⁡(12,18)=6 the gcd of open paren 12 comma 18 close paren equals 6.

For gcd⁡(48,180) the gcd of open paren 48 comma 180 close paren: The divisors of 48 48 are {1,2,3,4,6,8,12,16,24,48} the set 1 comma 2 comma 3 comma 4 comma 6 comma 8 comma 12 comma 16 comma 24 comma 48 and the divisors of 180 180 are {1,2,3,4,5,6,9,10,12,15,18,20,30,36,45,60,90,180} the set 1 comma 2 comma 3 comma 4 comma 5 comma 6 comma 9 comma 10 comma 12 comma 15 comma 18 comma 20 comma 30 comma 36 comma 45 comma 60 comma 90 comma 180. The greatest common divisor is 12 12.

Coprime Integers

Two integers a a and b b are called coprime (or relatively prime) if gcd⁡(a,b)=1 the gcd of open paren a comma b close paren equals 1. This means they share no common factor other than 1 1.

For example, gcd⁡(15,28)=1 the gcd of open paren 15 comma 28 close paren equals 1 since 15=3⋅5 15 equals 3 times 5 and 28=22⋅7 28 equals 2 squared times 7 share no prime factors. Coprimality is central to modular arithmetic: an integer a a has a multiplicative inverse modulo n n if and only if gcd⁡(a,n)=1 the gcd of open paren a comma n close paren equals 1.

Properties of the GCD

The greatest common divisor satisfies several fundamental properties that make it amenable to computation and theoretical analysis.

Basic Properties

For any integers a a, b b, and c c:

Commutativity: gcd⁡(a,b)=gcd⁡(b,a) the gcd of open paren a comma b close paren equals the gcd of open paren b comma a close paren

Associativity: gcd⁡(a,gcd⁡(b,c))=gcd⁡(gcd⁡(a,b),c) the gcd of open paren a comma the gcd of open paren b comma c close paren close paren equals the gcd of open paren the gcd of open paren a comma b close paren comma c close paren

Absolute values: gcd⁡(a,b)=gcd⁡(|a|,|b|) the gcd of open paren a comma b close paren equals the gcd of open paren the absolute value of a comma the absolute value of b close paren

Idempotence: gcd⁡(a,a)=|a| the gcd of open paren a comma a close paren equals the absolute value of a

Identity: gcd⁡(a,0)=|a| the gcd of open paren a comma 0 close paren equals the absolute value of a

The Key Divisibility Property

The property that makes the Euclidean algorithm work is:

gcd⁡(a,b)=gcd⁡(b,a−q⁢b) the gcd of open paren a comma b close paren equals the gcd of open paren b comma a minus q b close paren for any integer q q

In particular, if a=q⁢b+r a equals q b plus r (division with remainder), then:

gcd⁡(a,b)=gcd⁡(b,r) the gcd of open paren a comma b close paren equals the gcd of open paren b comma r close paren

This is because any common divisor of a a and b b must also divide r=a−q⁢b r equals a minus q b and conversely, any common divisor of b b and r r must divide a=q⁢b+r a equals q b plus r Thus the set of common divisors is the same, and so is the greatest one.

GCD and Prime Factorization

If we know the prime factorizations of a a and b b we can compute the GCD by taking the minimum exponent for each prime:

If a=p1a1⁢p2a2⋯pkak a equals p sub 1 raised to the a sub 1 power p sub 2 raised to the a sub 2 power dot dot dot p sub k raised to the a sub k power and b=p1b1⁢p2b2⋯pkbk b equals p sub 1 raised to the b sub 1 power p sub 2 raised to the b sub 2 power dot dot dot p sub k raised to the b sub k power then:

gcd⁡(a,b)=p1min⁡(a1,b1)⁢p2min⁡(a2,b2)⋯pkmin⁡(ak,bk) the gcd of open paren a comma b close paren equals p sub 1 raised to the min of open paren a sub 1 comma b sub 1 close paren power p sub 2 raised to the min of open paren a sub 2 comma b sub 2 close paren power dot dot dot p sub k raised to the min of open paren a sub k comma b sub k close paren power

For example, 48=24⋅3 48 equals 2 to the fourth power times 3 and 180=22⋅32⋅5 180 equals 2 squared times 3 squared times 5, so
gcd⁡(48,180)=2min⁡(4,2)⋅3min⁡(1,2)⋅5min⁡(0,1)=22⋅3=12 the gcd of open paren 48 comma 180 close paren equals 2 raised to the min of open paren 4 comma 2 close paren power times 3 raised to the min of open paren 1 comma 2 close paren power times 5 raised to the min of open paren 0 comma 1 close paren power equals 2 squared times 3 equals 12.

While this formula is conceptually clear, factoring large integers is computationally hard. The Euclidean algorithm provides a much faster way to compute the GCD without factoring.

The Euclidean Algorithm

The Division Algorithm

The Euclidean algorithm relies on the division algorithm, which states: for any integers a a and b b with b>0 b is greater than 0 there exist unique integers q q (quotient) and r r (remainder) such that:

a=q⁢b+rwhere 0≤r<b a equals q b plus r where 0 is less than or equal to r is less than b

We write r=a⁢mod⁡(b) r equals a mod b for the remainder.

Algorithm Description

To compute gcd⁡(a,b) the gcd of open paren a comma b close paren with a≥b>0 a is greater than or equal to b is greater than 0 repeatedly apply the key property:

a=q1⁢b+r10≤r1<bb=q2⁢r1+r20≤r2<r1r1=q3⁢r2+r30≤r3<r2⋮rn−2=qn⁢rn−1+rn0≤rn<rn−1rn−1=qn+1⁢rn+0 6 lines Line 1: a equals q sub 1 b plus r sub 1 0 is less than or equal to r sub 1 is less than b Line 2: b equals q sub 2 r sub 1 plus r sub 2 0 is less than or equal to r sub 2 is less than r sub 1 Line 3: r sub 1 equals q sub 3 r sub 2 plus r sub 3 0 is less than or equal to r sub 3 is less than r sub 2 Line 4: blank vertical ellipsis blank Line 5: r sub n minus 2 equals q sub n r sub n minus 1 plus r sub n 0 is less than or equal to r sub n is less than r sub n minus 1 Line 6: r sub n minus 1 equals q sub n plus 1 r sub n plus 0 blank

The sequence of remainders b>r1>r2>⋯≥0 b is greater than r sub 1 is greater than r sub 2 is greater than dot dot dot is greater than or equal to 0 is strictly decreasing and bounded below by zero, so it must eventually reach zero. The last nonzero remainder rn r sub n is the GCD.

gcd⁡(a,b)=gcd⁡(b,r1)=gcd⁡(r1,r2)=⋯=gcd⁡(rn−1,rn)=gcd⁡(rn,0)=rn the gcd of open paren a comma b close paren equals the gcd of open paren b comma r sub 1 close paren equals the gcd of open paren r sub 1 comma r sub 2 close paren equals dot dot dot equals the gcd of open paren r sub n minus 1 comma r sub n close paren equals the gcd of open paren r sub n comma 0 close paren equals r sub n

Bezouts Identity

One of the most important theoretical results about the GCD is Bezouts identity:

For any integers a a and b b (not both zero), there exist integers x x and y y such that:

a⁢x+b⁢y=gcd⁡(a,b) a x plus b y equals the gcd of open paren a comma b close paren

The integers x x and y y are called Bezout coefficients. They are not unique: if (x0,y0) open paren x sub 0 comma y sub 0 close paren is one solution, then (x0+k⁢b/d,y0−k⁢a/d) open paren x sub 0 plus k b divided by d comma y sub 0 minus k a divided by d close paren is also a solution for any integer k k where d=gcd⁡(a,b) d equals the gcd of open paren a comma b close paren

Bezouts identity has a powerful corollary: gcd⁡(a,b) the gcd of open paren a comma b close paren is the smallest positive integer that can be expressed as a linear combination a⁢x+b⁢y a x plus b y with integer coefficients.

The Extended Euclidean Algorithm

The extended Euclidean algorithm computes not only gcd⁡(a,b) the gcd of open paren a comma b close paren but also the Bezout coefficients x x and y y The idea is to track how each remainder can be expressed as a linear combination of a a and b b

We maintain the invariant that at each step i i we have ri=a⋅xi+b⋅yi r sub i equals a times x sub i plus b times y sub i. Starting with:

r0=a=a⋅1+b⋅0⇒(x0,y0)=(1,0) r sub 0 equals a equals a times 1 plus b times 0 right double arrow open paren x sub 0 comma y sub 0 close paren equals open paren 1 comma 0 close paren

r1=b=a⋅0+b⋅1⇒(x1,y1)=(0,1) r sub 1 equals b equals a times 0 plus b times 1 right double arrow open paren x sub 1 comma y sub 1 close paren equals open paren 0 comma 1 close paren

At each step, if ri−1=qi⁢ri+ri+1 r sub i minus 1 equals q sub i r sub i plus r sub i plus 1 then ri+1=ri−1−qi⁢ri r sub i plus 1 equals r sub i minus 1 minus q sub i r sub i so:

xi+1=xi−1−qi⁢xi,yi+1=yi−1−qi⁢yi x sub i plus 1 equals x sub i minus 1 minus q sub i x sub i comma y sub i plus 1 equals y sub i minus 1 minus q sub i y sub i

Applications

Reducing Fractions

To reduce a fraction ab a over b to lowest terms, divide both numerator and denominator by gcd⁡(a,b) the gcd of open paren a comma b close paren

ab=a/gcd⁡(a,b)b/gcd⁡(a,b) a over b equals the fraction with numerator a divided by the gcd of open paren a comma b close paren and denominator b divided by the gcd of open paren a comma b close paren

The resulting fraction is in lowest terms because the new numerator and denominator are coprime.

Modular Multiplicative Inverse

In modular arithmetic, we often need to find a−1⁢(mod⁡(n)) a to the negative 1 power times mod n the multiplicative inverse of a a modulo n n This inverse exists if and only if gcd⁡(a,n)=1 the gcd of open paren a comma n close paren equals 1

By Bezouts identity, if gcd⁡(a,n)=1 the gcd of open paren a comma n close paren equals 1 there exist x x and y y with a⁢x+n⁢y=1 a x plus n y equals 1 Taking this modulo n n

a⁢x≡1⁢(mod⁡(n)) a x is identical to 1 times mod n

So x x is the modular inverse. The extended Euclidean algorithm computes it efficiently.

Solving Linear Diophantine Equations

A linear Diophantine equation a⁢x+b⁢y=c a x plus b y equals c has integer solutions if and only if gcd⁡(a,b)⁢c∣ the gcd of open paren a comma b close paren c vertical bar

If d=gcd⁡(a,b) d equals the gcd of open paren a comma b close paren divides c c use the extended Euclidean algorithm to find x0,y0 x sub 0 comma y sub 0 with a⁢x0+b⁢y0=d a x sub 0 plus b y sub 0 equals d then multiply by c/d c divided by d to get a particular solution. The general solution is:

x=x0⁡(c/d)+(b/d)⁢t,y=y0⁢(c/d)−(a/d)⁢t x equals x sub 0 of open paren c divided by d close paren plus open paren b divided by d close paren times t comma y equals y sub 0 times open paren c divided by d close paren minus open paren a divided by d close paren times t for any integer t t

Cryptography: RSA

In RSA encryption, we choose two large primes p p and q q compute n=p⁢q n equals p q and ϕ⁡(n)=(p−1)⁢(q−1) phi of n equals open paren p minus 1 close paren times open paren q minus 1 close paren select an encryption exponent e e with gcd⁡(e,ϕ⁡(n))=1 the gcd of open paren e comma phi of n close paren equals 1 and compute the decryption exponent d=e−1⁢(mod⁡(ϕ⁡(n))) d equals e to the negative 1 power times mod phi of n using the extended Euclidean algorithm.

The security of RSA relies on the difficulty of factoring n n but the extended Euclidean algorithm makes key generation efficient.

Algorithm Complexity

The Euclidean algorithm is remarkably efficient. How many steps does it take to compute gcd⁡(a,b) the gcd of open paren a comma b close paren

Lames Theorem

The number of steps in the Euclidean algorithm never exceeds 5 5 times the number of digits in the smaller input.

More precisely, if the algorithm takes n n steps, then b≥Fn+1 b is greater than or equal to F sub n plus 1 where Fk F sub k is the k kth Fibonacci number. Since Fn≈ϕn/5 F sub n almost equals phi to the n-th power divided by the square root of 5 where ϕ=(1+5)/2 phi equals open paren 1 plus the square root of 5 close paren divided by 2 is the golden ratio, the number of steps is O⁢(log⁡b) O times log b

The worst case occurs when a a and b b are consecutive Fibonacci numbers. For example, gcd⁡(Fn+1,Fn) the gcd of open paren F sub n plus 1 comma F sub n close paren requires exactly n n divisions, each with quotient 1 1

Bit Complexity

If a a and b b have at most n n bits, the Euclidean algorithm runs in O⁡(n) O of n divisions. Each division of n nbit numbers takes O⁡(n2) O of open paren n squared close paren bit operations with schoolbook arithmetic, giving a total of O⁡(n3) O of open paren n cubed close paren bit operations. More sophisticated analysis shows the algorithm actually runs in O⁡(n2) O of open paren n squared close paren bit operations.

Relationship with Least Common Multiple

The least common multiple lcm⁡(a,b) the lcm of open paren a comma b close paren is the smallest positive integer divisible by both a a and b b The GCD and LCM are related by a beautiful formula:

gcd⁡(a,b)⋅lcm⁡(a,b)=|a⁢b| the gcd of open paren a comma b close paren times the lcm of open paren a comma b close paren equals the absolute value of a b

This can be understood through prime factorizations: if the GCD takes the minimum exponent for each prime, the LCM takes the maximum. For any prime p p with exponents α alpha in a a and β beta in b b

min⁡(α,β)+max⁡(α,β)=α+β min of open paren alpha comma beta close paren plus max of open paren alpha comma beta close paren equals alpha plus beta

This allows us to compute the LCM efficiently using the GCD: lcm⁡(a,b)=|a⁢b|gcd⁡(a,b) the lcm of open paren a comma b close paren equals the fraction with numerator the absolute value of a b and denominator the gcd of open paren a comma b close paren

Generalizations

The GCD extends naturally to more than two integers:

gcd⁡(a1,a2,…,an)=gcd⁡(a1,gcd⁡(a2,…,an)) the gcd of open paren a sub 1 comma a sub 2 comma dot dot dot comma a sub n close paren equals the gcd of open paren a sub 1 comma the gcd of open paren a sub 2 comma dot dot dot comma a sub n close paren close paren

The concept also generalizes to other algebraic structures. In polynomial rings, we can define the GCD of polynomials and compute it using the polynomial version of the Euclidean algorithm. In abstract algebra, the GCD is generalized to principal ideal domains.

Connection to Other Concepts

The GCD and Euclidean algorithm connect to many areas of mathematics:

In continued fractions, the quotients q1,q2,… q sub 1 comma q sub 2 comma dot dot dot from the Euclidean algorithm give the continued fraction expansion of a/b a divided by b

Eulers totient function ϕ⁡(n) phi of n counts integers from 1 1 to n n that are coprime to n n It is fundamental to Eulers theorem and RSA.

The Chinese Remainder Theorem provides a way to solve systems of congruences, and its proof uses the extended Euclidean algorithm to construct solutions.

In abstract algebra, the integers form a Euclidean domain, and the Euclidean algorithm provides the computational foundation for the theory of principal ideal domains.

Summary

The greatest common divisor gcd⁡(a,b) the gcd of open paren a comma b close paren is the largest positive integer dividing both a a and b b Two integers are coprime when their GCD is 1 1

The Euclidean algorithm computes the GCD through repeated division: gcd⁡(a,b)=gcd⁡(b,a⁢mod⁡(b)) the gcd of open paren a comma b close paren equals the gcd of open paren b comma a mod b close paren It terminates when the remainder is zero, and the last nonzero remainder is the GCD. The algorithm is efficient, running in O⁡(log⁡min⁡(a,b)) O of open paren the log of min of open paren a comma b close paren close paren steps.

Bezouts identity states that gcd⁡(a,b) the gcd of open paren a comma b close paren can always be expressed as a⁢x+b⁢y a x plus b y for some integers x x and y y The extended Euclidean algorithm computes these coefficients alongside the GCD.

Applications include reducing fractions to lowest terms, computing modular inverses, solving linear Diophantine equations, and implementing cryptographic systems like RSA. The GCD is also related to the LCM by gcd⁡(a,b)⋅lcm⁡(a,b)=|a⁢b| the gcd of open paren a comma b close paren times the lcm of open paren a comma b close paren equals the absolute value of a b

The Euclidean algorithm is one of the oldest algorithms in mathematics, yet it remains fundamental to modern number theory, algebra, and computer science. Its elegance lies in reducing a complex problem to a sequence of simple operations, a principle that echoes throughout mathematics.