Number Theory: GCD, Euclid, Diophantus, and Bézout
17 Dec 2021
§ Motivation
One of the simplest Diophantine equations is of the form . The extended Euclidean algorithm can solve for the greatest common divisor (GCD) between two numbers. The algorithm happens to also solve the Diophantine equation of the form , where the coefficients and are the Bézout coefficients.
When working in a modular group, Bézout coefficients are useful in a computing the modular inverse of a number, i.e. given find such that .
§ Greatest Common Divisor
Greatest Common Divisor
The greatest common divisor (GCD) is the greatest divisor shared between two numbers.
We often write to denote the GCD between and , rather than writing .
Properties of the GCD:
- .
- , zero is divisible by any number.
- .
- if is odd.
- if odd.
These properties are extremely useful in an implementation called "binary GCD".
For example, in a C++ fixed-precision integer setting using GCC, we can use __builtin_ctz()
to compute case (3) extremely quickly in the case that using bit operations.
§ Euclidean Algorithm
If , then there exists integers satisfying the linear Diophantine equation:
Let . Choose such that is the least positive integer in . Now we want to show that .
-
Seeking a contradiction, assume . Then the division algorithm gives that for some integers where .
Then, . This results in and contradiction. The same argument applied on shows that .
-
Let , then there exists integers such that and . So , so . Then , and . Therefore, is the greatest common divisor.
Euclidean Algorithm
Given integers not both equal to zero, the Euclidean algorithm is defined as
Then, the value .
§ Bézout Coefficients
The extended Euclidean algorithm can be used to find Bézout coefficients, i.e. in a given we can find coefficients for the linear Diophantine equation satisfying
Extended Euclidean Algorithm
Following the same steps as in the regular Euclidean algorithm, we can find a solution to by rewriting each in terms of and .
The values and are our Bézout coefficients.
§ Modular Inverse
Modular Inverse
Given and , we say that is the modular inverse of mod iff
We also write . Keep in mind that we are in , therefore must also be an integer.
If we want to find the modular inverse of modulo , we can use the extended Euclidean algorithm on . Then, the Bézout coefficient corresponding to is congruent to the modular inverse of .