GCD, LCM and the Euclidean Algorithm

Study Sheet

GCD, LCM and the Euclidean Algorithm

Greatest common divisors, Bézout, and fast computation

Greatest Common Divisors

Concept
The gcd from factorizations

The greatest common divisor gcd⁡(a,b)\gcd(a, b) is the largest integer dividing both aa and bb. From prime factorizations, take each prime to the SMALLER of its two exponents: 84=22⋅3⋅784 = 2^2\cdot3\cdot7 and 126=2⋅32⋅7126 = 2\cdot3^2\cdot7 give gcd⁡(84,126)=2⋅3⋅7=42\gcd(84, 126) = 2\cdot3\cdot7 = 42. Numbers with gcd⁡=1\gcd = 1 are coprime.

A common divisor can use each prime no more often than both numbers allow — that is the minimum exponent — and by unique factorization it can use no other primes. Taking everything allowed builds the largest one, and every other common divisor is a sub-product of it. So "greatest" means more than "largest": every common divisor divides the gcd. Factoring is quick for small numbers and hopeless for large ones; the next section finds the gcd without factoring at all.

Example
The largest square tile
180 cm = 15 tiles of 1248
12 = gcd(180, 48) fits a whole number of times along both edges.

A floor measuring 180180 cm by 4848 cm is to be covered with equal square tiles, without cutting any. How large can the tiles be? A side ss works exactly when whole tiles fill both edges, that is, when s∣180s \mid 180 and s∣48s \mid 48. So the largest ss is gcd⁡(180,48)\gcd(180, 48). With 180=22⋅32⋅5180 = 2^2\cdot3^2\cdot5 and 48=24⋅348 = 2^4\cdot3, the minimum exponents give 22⋅3=122^2\cdot3 = 12. The floor takes 15×4=6015\times4 = 60 tiles of 1212 cm.

Example
gcd⁡(n,n+6)\gcd(n, n + 6)

What values can gcd⁡(n,n+6)\gcd(n, n + 6) take for positive integers nn? Any common divisor of nn and n+6n + 6 divides their difference 66, so the gcd is one of 1,2,3,61, 2, 3, 6. All four occur: gcd⁡(1,7)=1\gcd(1, 7) = 1, gcd⁡(2,8)=2\gcd(2, 8) = 2, gcd⁡(3,9)=3\gcd(3, 9) = 3, gcd⁡(6,12)=6\gcd(6, 12) = 6. The same difference trick shows that consecutive integers are always coprime.

Side note
Where it goes wrong

Taking the LARGER exponents. For 8484 and 126126 that gives 22⋅32⋅7=2522^2\cdot3^2\cdot7 = 252 — the lcm, not the gcd — and a gcd larger than both numbers is an immediate red flag. Common divisors are limited by the number that has fewer of each prime.

Try it
Try it: gcd from exponents

Find gcd⁡(25⋅32⋅7, 23⋅34⋅5)\gcd(2^5\cdot3^2\cdot7,\ 2^3\cdot3^4\cdot5).

Show the answer

Take each prime to its smaller exponent: 2min⁡(5,3)⋅3min⁡(2,4)⋅50⋅70=23⋅32=722^{\min(5,3)}\cdot3^{\min(2,4)}\cdot5^0\cdot7^0 = 2^3\cdot3^2 = \mathbf{72}. (55 and 77 each appear in only one number, so their minimum exponent is 00.)

The Euclidean Algorithm

Concept
Repeated remainders

Since gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b), dividing repeatedly and keeping remainders reaches gcd⁡(a,b)\gcd(a, b) as the last nonzero remainder. It takes at most about 5×5\times(number of digits) steps — far faster than factoring.

The key fact gcd⁡(a,b)=gcd⁡(b,r)\gcd(a, b) = \gcd(b, r) where a=bq+ra = bq + r holds because any common divisor of aa and bb divides r=a−bqr = a - bq, and any common divisor of bb and rr divides a=bq+ra = bq + r. The two pairs have the same common divisors, so the same greatest one. Geometrically, each step cuts the largest possible squares off an a×ba \times b rectangle; the last square to fit exactly is the largest square that tiles the whole rectangle.

Example
Compute gcd⁡(252,105)\gcd(252, 105)
1051054242252105
Two 105-squares, then two 42-squares, then two 21-squares tile the 252 × 105 rectangle.

Divide, then divide the divisor by the remainder: 252=2⋅105+42252 = 2\cdot105 + 42; 105=2⋅42+21105 = 2\cdot42 + 21; 42=2⋅21+042 = 2\cdot21 + 0. The pairs (252,105)(252, 105), (105,42)(105, 42), (42,21)(42, 21) all have the same gcd, and the last one is obviously 2121. So gcd⁡(252,105)=21\gcd(252, 105) = 21 — three divisions, no factoring.

Example
A gcd for every nn

Show gcd⁡(5n+3,3n+2)=1\gcd(5n + 3, 3n + 2) = 1 for every integer nn. Subtract to shrink, exactly as the algorithm does: gcd⁡(5n+3,3n+2)=gcd⁡(3n+2,2n+1)=gcd⁡(2n+1,n+1)=gcd⁡(n+1,n)=gcd⁡(n,1)=1\gcd(5n + 3, 3n + 2) = \gcd(3n + 2, 2n + 1) = \gcd(2n + 1, n + 1) = \gcd(n + 1, n) = \gcd(n, 1) = 1. Each step replaces the larger expression by its difference with the smaller, which keeps the gcd, until a constant is reached.

Example
Powers of 22 minus 11

Find gcd⁡(218−1,212−1)\gcd(2^{18} - 1, 2^{12} - 1). Divide: 218−1=26(212−1)+(26−1)2^{18} - 1 = 2^6(2^{12} - 1) + (2^6 - 1), so the gcd equals gcd⁡(212−1,26−1)\gcd(2^{12} - 1, 2^6 - 1). And 212−1=(26−1)(26+1)2^{12} - 1 = (2^6 - 1)(2^6 + 1) is a multiple of 26−12^6 - 1, so the gcd is 26−1=632^6 - 1 = 63. The exponents ran through Euclid's algorithm themselves: gcd⁡(18,12)=6\gcd(18, 12) = 6, and in general gcd⁡(2m−1,2n−1)=2gcd⁡(m,n)−1\gcd(2^m - 1, 2^n - 1) = 2^{\gcd(m, n)} - 1.

Side note
Where it goes wrong

Reading off the wrong number. In 252=2⋅105+42252 = 2\cdot105 + 42, 105=2⋅42+21105 = 2\cdot42 + 21, 42=2⋅21+042 = 2\cdot21 + 0 the answer is the last NONZERO remainder, 2121 — not the final 00, and not a quotient like 22. A quick check catches it: the answer must divide both original numbers.

Try it
Try it: Euclid

Compute gcd⁡(1071,462)\gcd(1071, 462) and write it as 1071x+462y1071x + 462y.

Show the answer

1071=2⋅462+1471071 = 2\cdot462 + 147; 462=3⋅147+21462 = 3\cdot147 + 21; 147=7⋅21147 = 7\cdot21. gcd⁡=21\gcd = \mathbf{21}. Backwards: 21=462−3⋅147=462−3(1071−2⋅462)=7⋅462−3⋅107121 = 462 - 3\cdot147 = 462 - 3(1071 - 2\cdot462) = \mathbf{7\cdot462 - 3\cdot1071}. Check: 3234−3213=213234 - 3213 = 21.

Bézout's Identity

Concept
The gcd is a combination

For integers a,ba, b not both zero there exist integers x,yx, y with ax+by=gcd⁡(a,b)ax + by = \gcd(a, b). In particular aa and bb are coprime exactly when ax+by=1ax + by = 1 is solvable. The numbers of the form ax+byax + by are precisely the multiples of gcd⁡(a,b)\gcd(a, b).

Running the Euclidean algorithm backwards produces xx and yy: each remainder is a combination of the two numbers before it, so substituting upward expresses the last remainder in terms of aa and bb. Every combination ax+byax + by is a multiple of the gcd (the gcd divides both terms), and since the gcd itself is a combination, so is every multiple of it. This identity is the hinge of the course: it gives modular inverses, Euclid's lemma, and hence unique factorization.

Example
Back-substitution

From 252=2⋅105+42252 = 2\cdot105 + 42 and 105=2⋅42+21105 = 2\cdot42 + 21: start from the last useful line, 21=105−2⋅4221 = 105 - 2\cdot42, then replace 4242 using the line above it, 42=252−2⋅10542 = 252 - 2\cdot105: 21=105−2(252−2⋅105)=5⋅105−2⋅25221 = 105 - 2(252 - 2\cdot105) = 5\cdot105 - 2\cdot252. Check: 525−504=21525 - 504 = 21.

Example
Measuring with two jugs

With a 99-litre jug, a 44-litre jug and a tap, can you measure exactly 11 litre? Bézout says yes, because gcd⁡(9,4)=1\gcd(9, 4) = 1, and it tells you how: 1=9−2⋅41 = 9 - 2\cdot4. Fill the 99-litre jug, pour from it into the 44-litre jug twice (emptying the small jug in between), and 11 litre is left. Any whole number of litres up to 99 can be reached the same way.

Example
Which totals are reachable?

Which integers can be written as 84x+126y84x + 126y with x,yx, y integers? Every such number is a multiple of gcd⁡(84,126)=42\gcd(84, 126) = 42, since 4242 divides both terms. Conversely 42=2⋅84−12642 = 2\cdot84 - 126 is itself of this form, so every multiple 42m=84(2m)+126(−m)42m = 84(2m) + 126(-m) is too. The reachable totals are exactly the multiples of 4242; for example 100100 is not reachable.

Side note
Where it goes wrong

Assuming the coefficients are unique. 21=5⋅105−2⋅25221 = 5\cdot105 - 2\cdot252, but also 21=−7⋅105+3⋅25221 = -7\cdot105 + 3\cdot252: adding 0=(−12)⋅105+5⋅2520 = (-12)\cdot105 + 5\cdot252 changes (x,y)(x, y) without changing the total. There are infinitely many pairs, a fact the Diophantine topic turns into a formula.

Try it
Try it: Fibonacci neighbours

Write 1=gcd⁡(89,55)1 = \gcd(89, 55) as 89x+55y89x + 55y.

Show the answer

Euclid: 89=55+3489 = 55 + 34, 55=34+2155 = 34 + 21, 34=21+1334 = 21 + 13, 21=13+821 = 13 + 8, 13=8+513 = 8 + 5, 8=5+38 = 5 + 3, 5=3+25 = 3 + 2, 3=2+13 = 2 + 1. Substituting back up the chain gives 1=34⋅55−21⋅891 = 34\cdot55 - 21\cdot89, so x=−21x = \mathbf{-21}, y=34y = \mathbf{34}. Check: 1870−1869=11870 - 1869 = 1.

LCM and the Product Formula

Concept
gcd⁡⋅lcm=ab\gcd\cdot\text{lcm} = ab

The least common multiple lcm(a,b)\text{lcm}(a, b) is the smallest positive integer divisible by both. For positive a,ba, b: gcd⁡(a,b)⋅lcm(a,b)=ab\gcd(a,b)\cdot\text{lcm}(a,b) = ab. In terms of prime exponents, the gcd takes the minimum exponent of each prime and the lcm the maximum.

For each prime the two exponents are some ee and ff, and min⁡(e,f)+max⁡(e,f)=e+f\min(e, f) + \max(e, f) = e + f: whatever the gcd does not take, the lcm does. Multiplying over all primes gives gcd⁡⋅lcm=ab\gcd\cdot\text{lcm} = ab. In the Venn picture of prime factors, the overlap multiplies to the gcd and the whole picture to the lcm, while aa and bb together count the overlap twice.

Example
Find the lcm
223375252105gcd = 3·7 = 21, lcm = 2·2·3·3·7·5 = 1260
Shared prime factors (the overlap) make the gcd; all of them together make the lcm.

Find lcm(252,105)\text{lcm}(252, 105). With the gcd already known, lcm=252⋅105/21=1260\text{lcm} = 252\cdot105/21 = 1260. Check by factoring: 252=22⋅32⋅7252 = 2^2\cdot3^2\cdot7 and 105=3⋅5⋅7105 = 3\cdot5\cdot7; the maximum exponents give 22⋅32⋅5⋅7=12602^2\cdot3^2\cdot5\cdot7 = 1260.

Example
Bells

Three bells ring every 1212, 1818 and 3030 minutes, and all ring together at noon. When do they next ring together? The minutes that work are the common multiples of 1212, 1818, 3030; the first is their lcm. From 12=22⋅312 = 2^2\cdot3, 18=2⋅3218 = 2\cdot3^2, 30=2⋅3⋅530 = 2\cdot3\cdot5, the maximum exponents give 22⋅32⋅5=1802^2\cdot3^2\cdot5 = 180 minutes: at 33 p.m.

Example
Pairs with a given gcd and lcm

How many pairs a≤ba \le b have gcd⁡=6\gcd = 6 and lcm=180\text{lcm} = 180? Write a=6a′a = 6a\prime, b=6b′b = 6b\prime with gcd⁡(a′,b′)=1\gcd(a\prime, b\prime) = 1. Then lcm=6a′b′=180\text{lcm} = 6a\prime b\prime = 180, so a′b′=30=2⋅3⋅5a\prime b\prime = 30 = 2\cdot3\cdot5 with coprime factors: each of the three primes goes wholly to a′a\prime or to b′b\prime. That is 23=82^3 = 8 ordered splits, 44 unordered: (6,180),(12,90),(18,60),(30,36)(6, 180), (12, 90), (18, 60), (30, 36).

Side note
Where it goes wrong

Applying gcd⁡⋅lcm=\gcd\cdot\text{lcm} = product to THREE numbers. It is false in general: gcd⁡(2,2,2)⋅lcm(2,2,2)=2⋅2=4≠8\gcd(2,2,2)\cdot\text{lcm}(2,2,2) = 2\cdot2 = 4 \ne 8. With three numbers the minimum and maximum exponents no longer account for the middle one.

Try it
Try it: from gcd and lcm

Two numbers have gcd⁡=6\gcd = 6 and lcm=180\text{lcm} = 180, and one of them is 3030. Find the other.

Show the answer

By the product formula the other is 6⋅180/30=366\cdot180/30 = \mathbf{36}. Check: gcd⁡(30,36)=6\gcd(30, 36) = 6 and lcm(30,36)=180\text{lcm}(30, 36) = 180 — it is one of the four pairs found above.

Why It Works

If a=bq+ra = bq + r, then a number dividing aa and bb divides a−bq=ra - bq = r, and a number dividing bb and rr divides bq+r=abq + r = a. So (a,b)(a, b) and (b,r)(b, r) have exactly the same common divisors and the same gcd. The remainders strictly decrease and stay non-negative, so the process ends with a pair (d,0)(d, 0), whose gcd is dd. Chaining the equalities, gcd⁡(a,b)=d\gcd(a, b) = d, the last nonzero remainder.

Let dd be the smallest positive number of the form ax+byax + by (well-ordering again). Divide aa by dd: a=dq+ra = dq + r with 0≤r<d0 \le r < d. Then r=a−dq=a(1−qx)+b(−qy)r = a - dq = a(1 - qx) + b(-qy) is also a combination, and it is smaller than dd, so it cannot be positive: r=0r = 0 and d∣ad \mid a. Likewise d∣bd \mid b. And any common divisor of aa and bb divides ax+by=dax + by = d. So dd is a common divisor that every common divisor divides — it is gcd⁡(a,b)\gcd(a, b), written as a combination.