Numerical Example
We have seen in the theorem, we can represent the greatest common divisor of two integers as a linear combination of them, and it is actually the smallest positive linear combination. The question is, how can we find such a linear combination, and how can we find the greatest common divisor efficiently?
We can take two numbers and as an example. One naive idea is use subtraction repeatedly to find make the number smaller but remains positive. The following table shows the process:
| Step | |||
|---|---|---|---|
| 1 | 324 | 148 | 176 |
| 2 | 176 | 148 | 28 |
| 3 | 148 | 28 | 120 |
| 4 | 120 | 28 | 92 |
| 5 | 92 | 28 | 64 |
| 6 | 64 | 28 | 36 |
| 7 | 36 | 28 | 8 |
| 8 | 28 | 8 | 20 |
| 9 | 20 | 8 | 12 |
| 10 | 12 | 8 | 4 |
| 11 | 8 | 4 | 4 |
| 12 | 4 | 4 | 0 |
As the process terminates, we find the greatest common divisor of and is , and we took steps to find it. This methodology will always work, but it is not very efficient.
The Euclidean Algorithm is an improved version of the above process, it reduces the number of steps needed to find the greatest common divisor, with the price of doing division instead of subtraction. The following table shows the process of the Euclidean Algorithm:
| Step | ||||
|---|---|---|---|---|
| 1 | 324 | 148 | 324 = 148 * 2 + 28 | 28 |
| 2 | 148 | 28 | 148 = 28 * 5 + 8 | 8 |
| 3 | 28 | 8 | 28 = 8 * 3 + 4 | 4 |
| 4 | 8 | 4 | 8 = 4 * 2 + 0 | 0 |
With the Euclidean Algorithm, we only need steps to find the greatest common divisor of and . The process is replacing the dividend with the divisor, and the divisor with the remainder, and repeat the process until the remainder becomes . The greatest common divisor is the last non-zero remainder.
Euclidean Algorithm
The Euclidean Algorithm can be formally stated as follows:
Theorem
Let and be two integers, with and both positive. Then the greatest common divisor of and can be found by the following process:
The greatest common divisor of and is .
Proof of Correctness
The idea this time is to show the greatest common divisor of the pairs of numbers remain the same throughout the process. The process in the Euclidean Algorithm is recursive, so it is sufficient to show the invariance of the greatest common divisor for one step. We state it as a lemma:
Lemma
Let be positive integers, and let such that , and . Then the greatest common divisor of and is the same as the greatest common divisor of and .
Proof of Lemma
We will show the pair and share the same exact set of divisors. We will show two directions:
- If and , then .
- If and , then .
The first direction is easy, if and , then by this property.
For the second direction is less obvious, if and , then we can write and . Then we have:
This implies .
Therefore, the greatest common divisor of and is the same as the greatest common divisor of and .
Warning
Make sure you know how to prove this property.
Warning
GCD is not in general stable under linear combinations, find examples such that:
Can you figure out a condition on such that the equality holds?
Proof of Theorem
This completes the proof of the Euclidean Algorithm.
Backtracking to Find the Linear Combination
As we mentioned in the lecture, the Euclidean Algorithm not only returns the greatest common divisor, but also the linear combination of the two numbers. We can backtracking the table to find the linear combination:
You could also backtraking the first table to find the linear combination.