Discrete Structures
§11 Greatest Common Divisors and Euclid's Algorithm
7 min read
The greatest common divisor can be found without listing every divisor. The key is to replace a pair of integers by a smaller pair with exactly the same common divisors, then repeat.
Greatest common divisors
Let and suppose they are not both zero. Their greatest common divisor, written , is the greatest positive integer that divides both and . Equivalently, means
The absolute value in the last condition accounts for negative common divisors. In particular, for ,
The pair is excluded because every nonzero integer divides both entries, so there is no greatest positive common divisor.
For example, the positive common divisors of and are , so
Why listing divisors does not scale
For a positive integer , a direct method tests possible divisors up to . A divisor found below that point also reveals its complementary divisor , so the method still recovers divisors larger than . If has binary input length , then , while the number of trial divisions is roughly
That is exponential in the number of input bits. Euclid's algorithm avoids this divisor search.
The division algorithm
For every with , there are unique integers and such that
The integer is the quotient, and is the remainder. The remainder condition is part of the theorem; without it, many different pairs would satisfy .
Existence
First suppose . If , long division—or repeated subtraction of —produces a quotient and a remainder in the required range. If , choose a nonnegative integer large enough that . Apply the first case to :
Subtracting gives . If , apply the positive-divisor case to and rewrite as . These cases cover every nonzero integer .
Negative-dividend example
The quotient is allowed to be negative. For and ,
Thus the quotient is and the remainder is .
Uniqueness proof
Suppose two quotient–remainder pairs both satisfy the theorem:
Subtract the two equations:
Therefore . The remainder bounds also give
The only multiple of strictly between and is . Hence , so . Returning to the subtracted equation and using then gives
Both the quotient and the remainder are unique.
The gcd reduction theorem
Let , with . If
then
The theorem does more than preserve the value of the gcd: the two pairs have exactly the same common divisors.
Proof by matching common-divisor sets
Let
We prove both inclusions.
Take . Then and . Since a divisor of also divides , the difference property gives
Thus divides both and , so . Therefore .
Conversely, take . Then and , so divides their integer linear combination
Thus divides both and , so . Therefore .
The two inclusions show
so the greatest positive element of each set is the same. Hence .
Euclid's algorithm
For positive inputs, set and , then repeatedly apply the division algorithm:
Each step preserves the gcd. Stop at the first index for which , then return the positive divisor . If the first division already has remainder zero, this returns .
Why it terminates
Every nonzero remainder is a nonnegative integer strictly smaller than the preceding positive divisor. If the first remainder is not already zero, the positive remainders form a strictly decreasing sequence. Such a sequence cannot continue forever, so a zero remainder must eventually appear.
If is the first zero remainder, then the final division has positive divisor . Repeated use of the reduction theorem gives
Example:
The last nonzero remainder is , so
Bézout's identity
Let be not both zero, and let . Bézout's identity says that there are integers and such that
The numbers and are called Bézout coefficients. They need not be positive or unique.
Why back-substitution works
Each Euclidean step can be rearranged as
The starting values and are already integer linear combinations of and . The displayed equation shows inductively that every later remainder is also an integer linear combination of and . Since the divisor immediately before the first zero remainder equals , reversing the Euclidean steps expresses as .
The zero-input cases are direct. If , then : choose when , and when . If , choose when , and when . When both inputs are nonzero, apply the positive-input argument to and , then change the corresponding coefficient signs when an original input is negative. This completes the proof for all integer inputs that are not both zero.
For the smaller example above,
Thus one pair of Bézout coefficients for and is , .
Extended example:
Run Euclid's algorithm:
The last nonzero remainder is , so . Now substitute backward:
Therefore,
This simultaneously verifies the gcd and gives Bézout coefficients and .
0 reads
Last edited