Finding the largest number that divides both and by factoring them means discovering that , and factoring is slow work that gets slower very quickly as numbers grow.
The previous lesson closed the counting half of the course; this one opens the second half, on the structure of the integers. Recall that means for some integer , and that a number dividing and divides every combination . This lesson proves that division leaves a well defined remainder, turns that into Euclid's algorithm for the greatest common divisor, which needs no factoring, shows why it is fast, and runs it backwards.
Division with remainder
Dividing by gives with remainder , and school arithmetic takes for granted that the answer always exists and is the only one. Both halves need proof, because everything here rests on them.
The division algorithm. For integers and with there are unique integers and with and . The number is the quotient and the remainder, written .
For existence, let be the set of non-negative numbers of the form , with any integer. It is not empty, since gives . The well-ordering principle, that every non-empty set of non-negative integers has a least element, is induction in another form, and it gives a smallest member of , say . Then . If , then would be a smaller member of , which is impossible. So .
For uniqueness, suppose with both remainders between and . Then , which lies strictly between and . The only multiple of there is , so and .
The condition matters for negative : , so . Programming languages often disagree. In C and JavaScript -17 % 5 gives , because they round the quotient towards zero, and code that assumes remainders are never negative breaks on exactly this case.
The greatest common divisor
For integers and , not both zero, the greatest common divisor is the largest integer dividing both. It exists because is a common divisor and no divisor of a non-zero number exceeds its size. So , , and , since every integer divides . Numbers whose is are coprime.
Listing divisors fails for large numbers, and factoring first only moves the difficulty, since no fast method of factoring is known; a later lesson rests a whole cryptosystem on that fact. The way round it is one observation about remainders.
The key lemma. If for integers and , then and have exactly the same common divisors as and . In particular .
The proof is divisibility of combinations and nothing else. If and , then divides , which is . If and , then divides , which is . So the two pairs share one set of common divisors, and one set has one largest member. The lemma does not need to be the remainder, or even positive; any will do, a freedom used below.
With it reads , and it replaces a pair by a strictly smaller one, since .
Euclid's algorithm
Apply the lemma over and over. Divide by , divide by the remainder, and keep dividing each divisor by the remainder it left, until a remainder is . The last non-zero remainder is the greatest common divisor: the final pair is , , and the lemma says the never changed on the way. The process stops because the remainders are a strictly decreasing sequence of non-negative integers.
This is Propositions 1 and 2 of Book VII of Euclid's Elements, written around 300 BC. Euclid works with lengths, taking the smaller from the larger until it no longer fits, which is division done by repeated subtraction. Donald Knuth called it the granddaddy of all algorithms, the oldest nontrivial one still in use, and it still runs inside the software that reduces fractions and generates encryption keys.
Example. Find .
Each row divides by , and the next row moves and the remainder up one place:
| quotient | remainder | ||
|---|---|---|---|
The last non-zero remainder is , so , after six divisions and without meeting the prime . Check: and , and since is prime and does not divide , nothing larger is shared.
Now you. Find , and count the divisions.
Answer
, , , , , . Six divisions, and . Check: and .
Euclid on symbols
The algorithm does not need numbers. Whenever one expression is a multiple of another plus something simpler, the lemma applies, and a question about infinitely many pairs becomes a few lines of algebra.
Example. Prove that is in lowest terms for every positive integer . This was the first problem of the first International Mathematical Olympiad, held in Romania in 1959.
Lowest terms means the of top and bottom is . Divide as if the expressions were numbers: and . By the lemma twice, . At the fraction is , and both are prime.
Now you. Show that is always or , and say for which it is .
Answer
Write . The lemma allows any quotient, so , which is or . It is exactly when , that is for ; at the pair is and .
Why it is fast
Six divisions for four digit numbers is typical, and there is a quick reason. If a remainder is at most half the divisor before it, the next is smaller still; if it is more than half, the next quotient is and the next remainder is the difference, less than half. Either way every two divisions at least halve the numbers, so the work grows with the number of digits, not with the size of the numbers.
The exact worst case was found by Gabriel Lamé in 1844, and it is the Fibonacci numbers, , and , the sequence that earlier lessons on recurrences solved with Binet's formula. Run the algorithm on and . Since with , every quotient is and every remainder is the previous Fibonacci number, until leaves and finishes with quotient . That is exactly divisions, shrinking as slowly as possible, and it ends at : consecutive Fibonacci numbers are coprime.
Lamé's theorem. If the algorithm on takes divisions, then and .
Read the run from the bottom up. The last non-zero remainder is at least . The number above it is a larger multiple of it, so at least . Every other number is the one below it times a quotient of at least , plus the one below that, so it is at least the sum of the two. That is the Fibonacci recurrence with in place of , and induction up the run gives the bounds , reaching at and at . Consecutive Fibonacci numbers meet every bound exactly, so no smaller pair takes as long.
The counts check out. The pair takes divisions, and a computer search over every pair with finds nothing longer; with the longest run is , at . Binet's formula makes grow like , and exceeds , so each extra digit of allows fewer than five more divisions: at most five times the number of digits of . For a digit the true maximum is , since is the largest Fibonacci number below .
Running it backwards
Each division can be solved for its remainder, and that makes the more than the largest common divisor: it is a combination of the two numbers.
Bézout's identity. For integers and , not both zero, there are integers and with .
The proof is the algorithm. The first two numbers of the run are combinations of and , trivially: and . Each later number is the one two places above minus a quotient times the one just above, and that is again a combination. By induction every remainder is a combination of and , the last non-zero one included. The name is Étienne Bézout's, for his 1779 work on polynomials, though Claude Bachet de Méziriac stated the integer version in 1624.
A computer carries and forwards like this, row by row. By hand it is easier to start at the bottom and substitute upwards, which is why the method, called extended Euclid, is described as running the algorithm backwards.
Example. Find integers and with .
The run is , , and , so the is . Solve the last useful division for , then replace each remainder by the division that produced it:
So and . Check: .
Now you. Find integers and with .
Answer
The run is , , , , , so the is . Substituting upwards, . So and , and indeed .
What Bézout's identity buys
Write . Any common divisor divides , so : the is greatest in the stronger sense that every common divisor divides it. The common divisors of and are exactly the divisors of .
It also settles which numbers are combinations. Since divides and , it divides every , and scaling the Bézout combination gives every multiple of . So has integer solutions exactly when , and the smallest positive combination is itself. For instance is solved by , , while has no solution, since does not divide . Solutions are never unique: adding to and subtracting from changes by , so also gives .
In particular and are coprime exactly when is a combination of them, since a common divisor of both divides any combination. A later lesson builds division in modular arithmetic on exactly this.
Its first real consequence, though, is about primes. A divisor of a product need not divide a factor: divides but divides neither nor . For a prime it must, and a prime that does not divide is coprime to it, so for some integers, which is the lever. That is Euclid's lemma, Proposition 30 of the same Book VII, and the next lesson proves it from Bézout's identity in three lines and builds on it the fundamental theorem of arithmetic: every integer greater than factors into primes in exactly one way.