Greatest Common Divisors

Chapter 7: Number Theory

Learning objectives

  • Define the greatest common divisor of two integers
  • Apply the Euclidean algorithm to compute gcd(a,b)\gcd(a, b)
  • Express the GCD as a linear combination via Bezout coefficients
  • Recognise when two integers are relatively prime

Why does a tiny rule from antiquity power the modern internet? Every time your browser shakes hands with a banking site, every time a credit card terminal signs a transaction, every time a Signal message lands on your phone, an algorithm credited to Euclid is running in the background. The greatest common divisor is the largest integer that divides two numbers cleanly, and the Euclidean algorithm for finding it is the oldest non-trivial algorithm still in everyday use. It is fast (logarithmic in the inputs), it is exact, and it is the gateway to Bezout coefficients, modular inverses, RSA key generation, and a dozen other corners of cryptography. Master this one tool and the rest of Chapter 7 falls into place.

The definition, sharpened

For integers aa and bb not both zero, d=gcd(a,b)d = \gcd(a, b) is the unique positive integer satisfying two conditions: (1) dad \mid a and dbd \mid b, and (2) every common divisor cc of aa and bb also divides dd. The second condition is stronger than “cdc \leq d” — it says dd is the maximum common divisor in the divisibility order, not merely in size. Two integers with gcd(a,b)=1\gcd(a, b) = 1 are called coprime (equivalently, relatively prime).

The Euclidean algorithm

The heart of the algorithm is a one-line observation. If a=bq+ra = bq + r with 0r<b0 \leq r < b, then any common divisor of aa and bb also divides r=abqr = a - bq, and vice versa. Therefore gcd(a,b)=gcd(b,r)\gcd(a, b) = \gcd(b, r). Iterate this rewriting and the remainders form a strictly decreasing sequence of non-negative integers, so the process must terminate. The last non-zero remainder is the GCD.

Worked example. gcd(252,105)\gcd(252, 105): 252=1052+42252 = 105 \cdot 2 + 42, 105=422+21105 = 42 \cdot 2 + 21, 42=212+042 = 21 \cdot 2 + 0. The last non-zero remainder is 2121, so gcd(252,105)=21\gcd(252, 105) = 21.

Bezout’s identity

For any integers a,ba, b not both zero, there exist integers s,ts, t with gcd(a,b)=sa+tb\gcd(a, b) = sa + tb. The integers s,ts, t are the Bezout coefficients and are not unique — they are recovered by reading the Euclidean algorithm backwards. Back-substituting in the example above: 21=105422=105(2521052)2=5105225221 = 105 - 42 \cdot 2 = 105 - (252 - 105 \cdot 2) \cdot 2 = 5 \cdot 105 - 2 \cdot 252. So (s,t)=(2,5)(s, t) = (-2, 5). Bezout’s identity is the bridge from the GCD to modular inverses: when gcd(a,n)=1\gcd(a, n) = 1, the coefficient ss in sa+tn=1sa + tn = 1 is exactly a1(modn)a^{-1} \pmod{n}.

Induction StepperInteractive figure — enable JavaScript to interact.

(The stepper above visualises an iterative algorithm. Use it as a mental model for stepping through the Euclidean algorithm: each step shrinks the pair (a,b)(a, b) until the second entry reaches zero.)

Pause and think: Before reading on, predict gcd(0,15)\gcd(0, 15). (Hint: every integer divides 00, so the question reduces to: what is the largest divisor of 1515?) Then predict gcd(15,15)\gcd(15, 15). The pattern that emerges is worth a sentence of explanation in your own words.

Try it

  • Predict first: how many division steps does the Euclidean algorithm take on gcd(89,55)\gcd(89, 55)? (Both are Fibonacci numbers — this is the slowest case for inputs of that size.) Then run it and count.
  • Compute gcd(1071,462)\gcd(1071, 462) by hand. Find Bezout coefficients s,ts, t such that gcd=1071s+462t\gcd = 1071s + 462t.
  • Prove without a calculator: for any positive integer nn, gcd(n,n+1)=1\gcd(n, n+1) = 1. Use the “subtract one multiple” trick that drives the algorithm.
  • Two-step puzzle: compute gcd(2101,261)=gcd(1023,63)\gcd(2^{10} - 1, 2^6 - 1) = \gcd(1023, 63). The answer has a clean closed form — can you guess gcd(2m1,2n1)\gcd(2^m - 1, 2^n - 1) in general?

A trap to watch for

The GCD and the least common multiple (LCM) are siblings, not synonyms. gcd(a,b)\gcd(a, b) is the largest integer dividing both; lcm(a,b)\text{lcm}(a, b) is the smallest positive integer divisible by both. They satisfy gcd(a,b)lcm(a,b)=ab\gcd(a, b) \cdot \text{lcm}(a, b) = |ab|, so once you know one you can recover the other — but they are not the same number. A frequent student error is to compute gcd(12,18)=6\gcd(12, 18) = 6 and report 66 as “the common multiple,” or to call lcm(12,18)=36\text{lcm}(12, 18) = 36 “the largest common factor.” If you find yourself unsure which one a problem wants, ask: am I being asked for the biggest divisor (GCD, sits below both) or the smallest multiple (LCM, sits above both)?

Code ChallengeInteractive figure — enable JavaScript to interact.

What you now know

You can compute gcd(a,b)\gcd(a, b) in logarithmic time, recover Bezout coefficients by back-substitution, and translate “coprime” into the equation sa+tb=1sa + tb = 1. In the next section, prime factorisation, Bezout’s identity is the key lemma that proves uniqueness: it is precisely why a prime pp dividing a product abab must divide one of the factors, and therefore why every integer has only one factorisation into primes.

References

  • Velleman, D. J. (2019). How to Prove It: A Structured Approach (3rd ed.). Cambridge University Press, §7.1.
  • Hardy, G. H., Wright, E. M. (2008). An Introduction to the Theory of Numbers (6th ed.). Oxford University Press, ch. 2.
  • Niven, I., Zuckerman, H. S., Montgomery, H. L. (1991). An Introduction to the Theory of Numbers (5th ed.). Wiley, ch. 1.
  • Rosen, K. H. (2010). Elementary Number Theory (6th ed.). Pearson, ch. 3.
  • Knuth, D. E. (1997). The Art of Computer Programming, Vol. 2: Seminumerical Algorithms (3rd ed.). Addison-Wesley, §4.5.2.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.