Academy
MathsNumber TheoryGcd LcmGCD and the Euclidean Algorithm

GCD and the Euclidean Algorithm

What does it mean for two numbers to share a divisor — and how do you find the largest one without factoring? The Euclidean Algorithm, proved from scratch.

What is the largest integer that divides both 252 and 105?

You could list all divisors of each and find the intersection. That works here. It doesn't work when the numbers are large. You need a better question: is there a structure to shared divisors that lets you compute without listing?

There is. It's 2300 years old.


Divisibility, precisely

For integers aa and b0b \neq 0, we say bb divides aa — written bab \mid a — if there exists an integer kk such that a=kba = kb.

Three facts do most of the work in this topic:

  • If dad \mid a and dbd \mid b, then dabd \mid a - b.
  • More generally: if dad \mid a and dbd \mid b, then dma+nbd \mid ma + nb for any integers m,nm, n.
  • This last one — divisibility is preserved under linear combinations — is the engine behind everything below.

GCD — what it is

The greatest common divisor gcd(a,b)\gcd(a, b) is the largest positive integer that divides both aa and bb.

Before computing anything, notice what this definition guarantees:

  • gcd(a,0)=a\gcd(a, 0) = a, because every integer divides 0.
  • gcd(a,a)=a\gcd(a, a) = a.
  • gcd(a,b)=gcd(b,a)\gcd(a, b) = \gcd(b, a) — order doesn't matter.
  • gcd(a,b)=gcd(a,b)\gcd(a, b) = \gcd(|a|, |b|) — sign doesn't matter.

The last special case worth naming: when gcd(a,b)=1\gcd(a, b) = 1, we call aa and bb coprime. They share no common factor larger than 1.


The key observation

Here is the fact that makes efficient computation possible:

gcd(a,b)=gcd(b, amodb)\gcd(a, b) = \gcd(b,\ a \bmod b)

Why? The Division Algorithm says: for any integers aa and b>0b > 0, there exist unique integers qq and rr with 0r<b0 \leq r < b such that a=qb+ra = qb + r.

Now let dd be any common divisor of aa and bb. Then dad \mid a and dqbd \mid qb, so daqb=rd \mid a - qb = r. So dd divides both bb and rr.

Run it backwards: any common divisor of bb and rr divides qb+r=aqb + r = a, so it divides both aa and bb.

The set of common divisors of (a,b)(a, b) and (b,r)(b, r) are identical. Their maxima — the GCDs — must be equal.


The Euclidean Algorithm

Apply the observation repeatedly. At each step, replace the larger argument with the remainder. The remainders strictly decrease toward 0. When you hit 0, the last nonzero remainder is the GCD.

Example. gcd(252,105)\gcd(252, 105):

252=2×105+42252 = 2 \times 105 + 42 105=2×42+21105 = 2 \times 42 + 21 42=2×21+042 = 2 \times 21 + 0

Answer: gcd(252,105)=21\gcd(252, 105) = 21.

Example. gcd(1071,462)\gcd(1071, 462):

1071=2×462+1471071 = 2 \times 462 + 147 462=3×147+21462 = 3 \times 147 + 21 147=7×21+0147 = 7 \times 21 + 0

Answer: gcd(1071,462)=21\gcd(1071, 462) = 21.


How fast does it terminate?

Each step replaces (a,b)(a, b) with (b,r)(b, r) where r<br < b. The second argument strictly decreases. But how many steps?

The worst case is consecutive Fibonacci numbers. Watch what happens with gcd(Fn+1,Fn)\gcd(F_{n+1}, F_n):

Fn+1=1Fn+Fn1F_{n+1} = 1 \cdot F_n + F_{n-1}

Each step peels off exactly one Fibonacci number. After nn steps you reach gcd(F2,F1)=gcd(1,1)=1\gcd(F_2, F_1) = \gcd(1, 1) = 1.

Since FnF_n grows like ϕn\phi^n, the number of steps is at most O(logϕ(min(a,b)))O(\log_\phi(\min(a,b))) — logarithmic in the input. Even for billion-digit numbers, the algorithm terminates in thousands of steps.


LCM — the companion

The least common multiple lcm(a,b)\text{lcm}(a, b) is the smallest positive integer divisible by both aa and bb.

The two quantities are linked:

gcd(a,b)lcm(a,b)=ab\gcd(a, b) \cdot \text{lcm}(a, b) = |a \cdot b|

Proof. Let d=gcd(a,b)d = \gcd(a, b). Write a=dpa = dp, b=dqb = dq where gcd(p,q)=1\gcd(p, q) = 1.

Claim: lcm(a,b)=dpq\text{lcm}(a, b) = dpq.

dpqdpq is divisible by a=dpa = dp and by b=dqb = dq. ✓

Now suppose LL is any common multiple of aa and bb. Write L=dpmL = dpm for some integer mm. Then dqdpmdq \mid dpm means qpmq \mid pm. Since gcd(p,q)=1\gcd(p, q) = 1, we get qmq \mid m, so L=dpqndpqL = dpqn \geq dpq.

Therefore lcm(a,b)=dpq\text{lcm}(a,b) = dpq and gcd(a,b)lcm(a,b)=ddpq=ab\gcd(a,b) \cdot \text{lcm}(a,b) = d \cdot dpq = ab. \blacksquare

Common mistake. This relation holds for two numbers only. gcd(a,b,c)lcm(a,b,c)abc\gcd(a,b,c) \cdot \text{lcm}(a,b,c) \neq abc in general. Check with a=b=c=2a=b=c=2.


Worked problems

Problem 1. If gcd(a,b)=12\gcd(a, b) = 12 and lcm(a,b)=1800\text{lcm}(a, b) = 1800, find all pairs (a,b)(a, b) with aba \leq b.

Write a=12pa = 12p, b=12qb = 12q with gcd(p,q)=1\gcd(p, q) = 1. Then lcm(a,b)=12pq=1800\text{lcm}(a,b) = 12pq = 1800, so pq=150=2×3×52pq = 150 = 2 \times 3 \times 5^2.

Coprime pairs (p,q)(p, q) with pqp \leq q and pq=150pq = 150:

ppqqgcd(p,q)\gcd(p,q)
11501 ✓
2751 ✓
3501 ✓
6251 ✓
10155 ✗

Pairs (a,b)(a, b): (12,1800)(12, 1800), (24,900)(24, 900), (36,600)(36, 600), (72,300)(72, 300).

Problem 2. Find gcd(n,n+1)\gcd(n, n+1) for any positive integer nn.

Any common divisor dd satisfies dnd \mid n and dn+1d \mid n+1, so d(n+1)n=1d \mid (n+1) - n = 1. Thus d=1d = 1.

Consecutive integers are always coprime.

Problem 3. Show that gcd(n,n+k)=gcd(n,k)\gcd(n, n+k) = \gcd(n, k).

gcd(n,n+k)=gcd(n,(n+k)n)=gcd(n,k)\gcd(n, n+k) = \gcd(n, (n+k) - n) = \gcd(n, k). The shift by nn is invisible.


What this page doesn't answer

The Euclidean Algorithm finds gcd(a,b)\gcd(a, b). It doesn't tell you what gcd(a,b)\gcd(a, b) is beyond "the largest common divisor." There's a deeper characterization — the GCD is the smallest positive integer expressible as a combination ax+byax + by.

That's Bezout's Identity, and it's where the real power lives.