GCDs and the Euclidean Algorithm
GCDs
Note
Common Divisor
A common divisor of two integers and is any integer such that both and .
Example. Which of , and are common divisors of and ?
Since and , is a common divisor; since and , is also a common divisor; but , so is not.
Note
GCD
A greatest common divisor (GCD) of two integers and is any integer such that
and
Here, both 6 and -6 are GCDs of 12 and 18.
Note
Standard GCD
The standard greatest common divisor of two integers and (not both 0), written as is the integer such that
and
Here,
Note
Coprime
Two integers and are coprime if they have no common divisors other than . In other words, .
Example. Which of the following pairs are coprime?
and are coprime; and are coprime; and are not coprime, since divides both; and are coprime, since the only divisors of are .
Note
Lemma 1
For any integer , .
Note
Lemma 2
For any integer , .
Suppose is the greatest divisor of and 0, therefore and ; .
Since we are looking at the greatest divisor, is the strongest candidate that satisfies . However, , so instead we can assume that . Since every integer divides 0, we know that , therefore we know that .




Properties of the Standard GCD
Lemmas 1 and 2 are actually just the first two of a family of useful properties of the standard GCD. Suppose , , and are integers; then the following all hold.
Note
Lemma 3
For any integers , and ,
Basically, is associative; if you need the GCD of three (or more) numbers, you can pair them up in whatever order you like and the answer does not change. The proof is a Topic 1 problem (Q12), so I'm not gonna write it here.
Example. Find the GCD of , and .
Pairing from the right,
Pairing from the left,
Both orders agree, so the GCD of all three numbers is .
Note
Lemma 4
For any integers , and ,
Basically, a shared factor can be pulled straight out the front of a ; just remember the absolute value, since the standard GCD is never negative.
Example. Find .
Notice that and share the factor , so
The same works for negative shared factors; .
Note
Lemma 5
For any integers , and , if and , then .
Basically, if divides a product but is coprime to one of the factors, then it has no choice but to divide the other factor; none of can "hide" inside , so all of it must land on .
Example. Suppose for some integer . What can we say about ?
Since , Lemma 5 tells us that . For instance, if , then , and indeed .
It is important to remember that Lemma 5 fails without the coprime condition. For example, , but , and sure enough and .
The Key Lemma
The last property is the engine behind the Euclidean Algorithm, so it gets its own proof.
Note
Lemma 6
For any integers , , and , if , then
Proof. We show that the pairs and have exactly the same common divisors; if the two sets of common divisors are identical, then in particular their greatest elements must be equal.
Suppose and . Since is an integer combination of and , we get ; so every common divisor of and is also a common divisor of and .
Conversely, suppose and . Since is an integer combination of and , we get ; so every common divisor of and is also a common divisor of and .
Hence the two pairs share the same common divisors, and therefore the same greatest common divisor; that is, .
Basically, replacing with its remainder on division by does not change the GCD; this lets us shrink a GCD problem into a smaller one over and over again until it becomes trivial, which is exactly what the Euclidean Algorithm does.
Example. Show that any two consecutive integers are coprime, and hence find .
For any integer we can write
so by Lemma 6,
Therefore any two consecutive integers are coprime; in particular, , with no arithmetic needed at all.
The Division Theorem
Note
The Division Theorem
For any integers and with , there exist unique integers and such that both
We call the quotient and the remainder when is divided by .
It is important to note here that must ALWAYS be at least 0, even if is negative;
Example. Find the quotient and remainder when is divided by ; is divided by ; is divided by ; and is divided by .
A common trap with negative is to write ; this is arithmetically true, but it is NOT the Division Theorem, because the remainder is negative. Instead, we push the quotient one further down to so that the remainder lands back in the range . Basically, for negative you round the quotient towards , not towards .
Proof. First suppose . Choose
i.e. is the largest integer less than or equal to . Then holds by construction, and since
multiplying through by and then subtracting gives
as required. The case works the same way, except we instead take .
For uniqueness, suppose there were a second solution with . Setting the two expressions for equal,
Since both remainders lie in the interval , their difference satisfies . But is an integer multiple of , and the only multiple of strictly between and is . Hence and ; the quotient and remainder are unique.
The Euclidean Algorithm
Note
The Euclidean Algorithm
The Euclidean Algorithm is a process that, given two integers and as inputs, efficiently outputs . The algorithm makes use of the Division Theorem, finding quotients and remainders iteratively in the following way:
The process terminates immediately after the th step, when the remainder is first found to be zero. The remainder at the th step is then the GCD of and . That is,
Basically, you divide by , then divide by the remainder, then divide that remainder by the new remainder, and so on; each line just feeds the two right-most numbers of the line above back into the Division Theorem. The remainders keep strictly shrinking, so eventually one hits , and the last non-zero remainder is the GCD.
Example. Use the Euclidean Algorithm to find .
The last non-zero remainder is , therefore .
Example. Use the Euclidean Algorithm to find .
The last non-zero remainder is , therefore ; i.e. and are coprime.
Example. Use the Euclidean Algorithm to find .
Even with bigger numbers the remainders collapse very quickly;
Therefore .
Negative Inputs
Since an integer and its negative have exactly the same divisors, we have
so the easiest way to deal with a negative input is to just drop the sign before starting.
Example. Find .
Dropping the sign first, , so
Therefore .
Alternatively, the algorithm works perfectly fine on the negative input directly, as long as every remainder stays in the range (recall that the Division Theorem forces even when the quotient goes negative);
Both routes agree that the answer is ; dropping the sign first is simply less error-prone.
Why the Algorithm Works
Note
Theorem
For any integer inputs and , the Euclidean Algorithm always outputs .
Proof. There are two things to show; that the process always terminates, and that it always returns the standard GCD.
For termination, notice that the remainders satisfy
so they form a strictly decreasing sequence of non-negative integers; such a sequence cannot go on forever, so a zero remainder must appear after at most steps.
For correctness, every line of the algorithm has exactly the form of Lemma 6, so each step replaces the current pair with a smaller pair that has the same GCD. If the algorithm terminates after steps, then
Notice how the very last pair hands us the answer for free; this is exactly why Lemma 2 matters.
More Examples
Example. Find .
Both and are consecutive Fibonacci numbers;
Therefore ; any two consecutive Fibonacci numbers are coprime. Notice how every quotient is until the final step, and the remainders are just the Fibonacci numbers marching back down (); consecutive Fibonacci numbers are actually the worst case input for the Euclidean Algorithm, forcing the maximum possible number of steps for numbers of their size.
Example. A rectangular floor is cm by cm. What is the side length of the largest square tile that can tile the floor exactly, and how many tiles are needed?
A square tile of side tiles the floor exactly if and only if and ; the largest such is precisely .
So the largest tile has side cm, and the number of tiles needed is
Therefore, the largest square tile is cm cm, and tiles are needed.