One of the simplest Diophantine equations is of the form ax+by=c.
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 ax+by=gcd(x,y), where the coefficients a and b 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 a find b such that ab≡n1.
The greatest common divisor (GCD) is the greatest divisor shared between two numbers.
We often write (a,b) to denote the GCD between a and b, rather than writing gcd(a,b).
Properties of the GCD:
(a,b)=(b,a).
(0,b)=b, zero is divisible by any number.
(n⋅a,n⋅b)=n⋅(a,b).
(2a,b)=(a,b) if b is odd.
(a,b)=(∣a−b∣,min(a,b)) if u,v 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 n=2k using bit operations.
If (b,c)=g, then there exists integers x0,y0 satisfying the linear Diophantine equation:
bx0+cy0=(b,c).
Let A={bx+cy,x,y∈Z}. Choose x0,y0 such that l=bx0+cy0 is the least positive integer in A. Now we want to show that l=g.
Seeking a contradiction, assume l∤b. Then the division algorithm gives that b=lq+r for some integers q,r where 0<r<l.
Then, r=b−lq=b−q(bx0+cy0)=b(1−qx0)+c(−qy0). This results in r∈A and 0<r<l, contradiction. The same argument applied on c shows that l∣c.
Let g=(b,c), then there exists integers B,C such that gB=b and gC=c. So l=bx0+cy0=g(Bx0+Cy0), so g∣l. Then g≤l, l∣b and l∣c. Therefore, l is the greatest common divisor.
Euclidean Algorithm
Given integers b,c not both equal to zero, the Euclidean algorithm is defined as
b=cq1+r1c=r1q2+r2r1=r2q3+r3rj−2=rj−1qj+rjrj−1=rjqj+1⋮r1∈(0…c)r2∈(0…r1)r3∈(0…r1)rj∈(0…rj−1)stop when rj+1=0.
The extended Euclidean algorithm can be used to find Bézout coefficients, i.e. in a given a,b we can find coefficients s,t for the linear Diophantine equation satisfying
s⋅a+t⋅b=(a,b).
Extended Euclidean Algorithm
Following the same steps as in the regular Euclidean algorithm, we can find a solution to bx0+cy0=(b,c) by rewriting each ri in terms of b and c.
Given a∈Zn and (a,n)=1, we say that b is the modular inverse of a mod n iff
a⋅b≡n1.
We also write b=a−1. Keep in mind that we are in Zn, therefore a−1 must also be an integer.
If we want to find the modular inverse of a modulo n, we can use the extended Euclidean algorithm on (a,n).
Then, the Bézout coefficient corresponding to a is congruent to the modular inverse of a.