Skip to content

The Euclidean algorithm for the GCD

4 min read

Short answer

Replace (a,b)(a,b) by (b,  a mod b)(b,\;a\bmod b) until the remainder is 00; the last non-zero remainder is gcd⁡(a,b)\gcd(a,b). It works because gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a,b)=\gcd(b,a\bmod b), and it needs far fewer steps than factoring.

Steps148 = 2 · 18 + 12218 = 1 · 12 + 6312 = 2 · 6 + 04The last non-zero remainder is6, so gcd(48, 18) = 6Each division shrinks the numbers quickly.
Each step replaces the pair by a smaller one with the same gcd.

Why it is correct

If a=qb+ra=qb+r, any common divisor of aa and bb divides r=a−qbr=a-qb, and any common divisor of bb and rr divides aa. So the two pairs have the same common divisors, hence the same greatest one.

Bezout and inverses

Working the steps backwards writes gcd⁡(a,b)=ax+by\gcd(a,b)=ax+by for integers x,yx,y (extended Euclid). When the gcd is 11, xx is the inverse of aa mod bb, which is how modular division is done.

Worked example

Find gcd⁡(252,105)\gcd(252,105) and use it to simplify 105252\tfrac{105}{252}.

252=2⋅105+42252=2\cdot105+42; 105=2⋅42+21105=2\cdot42+21; 42=2⋅21+042=2\cdot21+0. So gcd⁡=21\gcd=21. Dividing top and bottom by 2121: 105252=512\tfrac{105}{252}=\tfrac{5}{12}.

Try it yourself

Compute gcd⁡(1071,462)\gcd(1071,462).

Show the answer

1071=2⋅462+1471071=2\cdot462+147; 462=3⋅147+21462=3\cdot147+21; 147=7⋅21+0147=7\cdot21+0. The gcd is 2121.

Keep going with practice

Vidi's linear algebra lessons use the same repeated-reduction idea in Gaussian elimination.