Folioby Interconnected
Log InSign Up

Why the Euclidean algorithm finds the greatest common divisor: from subtraction to division

The recursion gcd(a,b) = gcd(b, a mod b) hides an invariant: the set of common divisors never changes. Starting from a naive subtraction-based method and upgrading to division, we derive Bezout's identity, the extended Euclidean algorithm, and modular inverses.

FO
Folio Official
March 1, 2026

The Euclidean algorithm, recorded in Euclid's Elements around 300 BCE, is one of the oldest algorithms known to humanity. Yet textbooks often present it as a bare recursion – gcd(a,b)=gcd(b,amodb)– without explaining why this operation reliably produces the greatest common divisor.

1 The definition of the greatest common divisor

Definition 1 (Greatest common divisor).
For nonzero integers a and b, the greatest common divisorgcd(a,b) is the largest positive integer that divides both a and b.
Remark 2.
Intuitively, gcd(a,b) collects all the prime factors that a and b share. For example, gcd(12,18)=6, since 12=22×3 and 18=2×32, and the common part is 2×3=6.

2 The naive approach: repeated subtraction

The simplest way to compute the greatest common divisor is by repeated subtraction.

Theorem 3 (Subtraction preserves the GCD).
If a>b>0, then gcd(a,b)=gcd(a−b,b).
Proof.
If d∣a and d∣b, then d∣(a−b). Conversely, if d∣(a−b) and d∣b, then d∣a. Thus the set of common divisors of (a,b) and (a−b,b) is the same, so their maxima coincide. □
Example 4.
Computing gcd(48,18) by subtraction:
gcd(48,18)=gcd(30,18)=gcd(12,18)=gcd(12,6)=gcd(6,6)=6

This method is correct but slow. When a and b are far apart, it can require an enormous number of steps (gcd(109,1) would need 109 subtractions).

3 The upgrade to division: why the algorithm is fast

Instead of subtracting one copy of b at a time, we subtract as many copies as possible in a single step – that is, we divide.

Theorem 5 (The core identity of the Euclidean algorithm).
If a≥b>0, then gcd(a,b)=gcd(b,amodb).
Proof.
Write a=qb+r with 0≤r<b. If d∣a and d∣b, then d∣(a−qb)=r. Conversely, if d∣b and d∣r, then d∣(qb+r)=a. Therefore gcd(a,b)=gcd(b,r). □
Remark 6.
The subtraction version reduces a by b in each step; the division version reduces a all the way to amodb in a single step, effectively performing q subtractions at once.
Theorem 7 (Complexity).
The Euclidean algorithm runs in O(log(min(a,b))) steps.
Remark 8.
Why logarithmic? The key observation is that amodb<a/2 always holds. If b≤a/2, then the remainder is less than b≤a/2. If b>a/2, then amodb=a−b<a/2. In either case, every two consecutive steps halve the larger argument, and the algorithm terminates in O(loga) steps.

4 Bézout's identity: the GCD as a linear combination

Theorem 9 (B\'ezout's identity).
For any integers a and b, there exist integers x and y such that
gcd(a,b)=ax+by.
Proof.
The coefficients x,y can be constructed by tracing the Euclidean algorithm in reverse. In the base case, gcd(d,0)=d=d⋅1+0⋅0. For the recursive step, suppose gcd(b,amodb)=bx′+(amodb)y′. Substituting amodb=a−⌊a/b⌋b gives gcd(a,b)=ay′+b(x′−⌊a/b⌋y′). □

5 The extended Euclidean algorithm

The constructive proof of Béextended Euclidean algorithm.

Example 10.
We compute gcd(111,30) and find integers x,y with 111x+30y=gcd(111,30).
  • 111=3×30+21

  • 30=1×21+9

  • 21=2×9+3

  • 9=3×3+0

So gcd(111,30)=3. Now we back-substitute:
  • 3=21−2×9

  • =21−2(30−21)=3×21−2×30

  • =3(111−3×30)−2×30=3×111−11×30

Check: 3×111−11×30=333−330=3.

6 Computing modular inverses

Theorem 11 (Modular inverse).
If gcd(a,n)=1, there exists an integer x satisfying ax≡1(modn). This x is called the modular inverse of a modulo n.
Proof.
By Béax+ny=1 for some integers x,y. Reducing both sides modulo n gives ax≡1(modn). □
Remark 12 (Modular inverses in competitive programming).
When p is prime and a≡0(modp), we have gcd(a,p)=1, so the extended Euclidean algorithm computes the inverse in O(logp) time. Compared with the alternative ap−2modp (via Fermat's little theorem), the extended Euclidean algorithm has a smaller constant factor and, crucially, also works when the modulus is not prime.

7 Takeaway

  • The Euclidean algorithm rests on the invariant that the set of common divisors is unchanged at each step.

  • Upgrading subtraction to division yields an O(log)-time algorithm.

  • Bé

  • The extended Euclidean algorithm turns this guarantee into an efficient procedure for computing modular inverses.

In the next article, we examine the "atoms" of the integers – the primes – and ask why the Fundamental Theorem of Arithmetic holds, and in what settings it fails.

Number TheoryMathematicsBetween the Lines
FO
Folio Official

Mathematics "between the lines" — exploring the intuition textbooks leave out, written in LaTeX on Folio.

1 followers·107 articles
Number Theory — Between the LinesPart 2 of 8
Previous
What does it mean to "divide evenly"? Why number theory begins with mod
Next
Why primes are the atoms of the integers: what the Fundamental Theorem of Arithmetic really says

Share your expertise with the world

Write articles with LaTeX support, build your audience, and earn from your knowledge.

Start Writing — It's Free

More from Folio Official

Folio Official·March 1, 2026

Why primes are the atoms of the integers: what the Fundamental Theorem of Arithmetic really says

Unique prime factorization is not as obvious as it seems. Through a counterexample in Z[sqrt(-5)], we witness uniqueness break down, then prove both the existence and uniqueness parts of the Fundamental Theorem, and survey the Sieve of Eratosthenes and the linear sieve.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

The Chinese Remainder Theorem: why simultaneous congruences can be merged

We view CRT as the number-theoretic incarnation of divide-and-conquer, prove the ring isomorphism Z/mnZ = Z/mZ x Z/nZ, and show how Garner's algorithm avoids overflow in practice.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

What does it mean to "divide evenly"? Why number theory begins with mod

The modular reduction map is a projection that deliberately discards information. Clocks, days of the week, and the competitive-programming modulus 10^9+7 all share the same algebraic structure. From the definition of divisibility through congruences to the ring Z/nZ, we uncover the reason number theory starts where it does.

Number TheoryMathematicsBetween the Lines
Folio Official·March 1, 2026

Number theory meets cryptography: why RSA works and what makes it secure

Every theorem in this series -- Fermat, Euler, the extended Euclidean algorithm, CRT -- corresponds to a component of the RSA cryptosystem. We trace each connection, work through a concrete example, and discuss the factoring barrier that underpins RSA's security.

Number TheoryMathematicsBetween the Lines