GCDs
Let's start off with what we know about divisibility:
divides () iff there is an integer such that .
Then, we know that for all ,
To put it crudely, we can say that everything is a divisor of itself, everything divides , and divides everything.
We also know some basic facts about divisibility:
- If and , then .
- If and , then for all and .
- For all , iff .
Note: The number of the form is called the linear combination of and .
As long as and are not , they will have their greatest common divisor, and we can write it as .
Euclid's Algorithm
We can find s with Euclid's Algorithm:
For ,
Okay, we know that , where is the quotient, and is the remainder. Notice that is if divides .
If something divides both and , then it also divides . And, if something divides and , then it divides , too.
An easy example is to find the of and :
.
We can think of the Euclidean Algorithm as a state machine.
States will be a pair of nonnegative integers, .
The start state is the original pair we want to find out the of, . In the above example, it is .
State transition rule is for .
And, here is the more interesting part: the preserved invariant is the itself, it stays constant. In the example, is , is also , and is also .
So, we can say that at any point is the same as the original value, .
We can write a short recursive function for it:
def gcd(x, y):
if y == 0:
print(f'The result is: {x}')
return x
print(f'Calculating gcd({x}, {y})...')
return gcd(y, x % y)
In this case gcd(26, 21)
gives the output of:
Calculating gcd(26, 21)...
Calculating gcd(21, 5)...
Calculating gcd(5, 1)...
The result is: 1
There is also this fact:
says that the greatest common divisor of and is the linear combination of and .
If that's the case, we can also reason that an integer is a linear combination of and iff it is a multiple of .
First, let's take a look at linear combinations more closely.
Let's say we want to write as a linear combination.
It has to be in the form: .
We can use Euclid's Algorithm, but let's go step by step:
|
|
|
|
|
|
|
|
|
|
|
|
You might already see the pattern.
Now, we're going to ignore the last row, and go reverse. We're going to rewrite everything with according to the remainder this time.
|
|
|
|
|
|
|
|
|
|
Let's rewrite the same thing once again, but we'll plug in the values as shown.
So, we can still write as , but let's plug in the value for :
Let's simplify it a bit:
.
And, even further:
.
Okay, now where are we going with this?
Let's continue plugging in values. This time for :
.
Now, simplify it: .
It's time to plug in the value for :
.
Simplify it: .
In case you zoned out at some point during this process, let me repeat what we have as the end result: .
That's exactly what we were looking for in the first place, a number of the form !
The and are also called Bézout's coefficients. It is Bézout's identity that says we can write the greatest common divisor as a linear combination:
If , then such that .
The method we used to find out and is called the Extended Euclidean Algorithm (the "Pulverizer").