Number Theory: GCD, Euclid, Diophantus, and Bézout

17 Dec 2021


§ Motivation

One of the simplest Diophantine equations is of the form ax+by=cax + 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)ax + by = \gcd(x, y), where the coefficients aa and bb 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 aa find bb such that abn1ab \equiv_n 1.

§ Greatest Common Divisor

Greatest Common Divisor

The greatest common divisor (GCD) is the greatest divisor shared between two numbers.

We often write (a,b)(a, b) to denote the GCD between aa and bb, rather than writing gcd(a,b)\gcd(a, b).

Properties of the GCD:

  1. (a,b)=(b,a)(a, b) = (b, a).
  2. (0,b)=b(0, b) = b, zero is divisible by any number.
  3. (na,nb)=n(a,b)(n\cdot a, n\cdot b) = n\cdot (a, b).
  4. (2a,b)=(a,b)(2a, b) = (a, b) if bb is odd.
  5. (a,b)=(ab,min(a,b))(a, b) = (\vert a - b\vert, \min(a, b)) if u,vu, 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=2kn = 2^k using bit operations.

§ Euclidean Algorithm

If (b,c)=g(b, c) = g, then there exists integers x0,y0x_0, y_0 satisfying the linear Diophantine equation:

bx0+cy0=(b,c).bx_0 + cy_0 = (b, c).

Let A={bx+cy,x,yZ}A = \{bx + cy, x, y \in \mathbb{Z}\}. Choose x0,y0x_0, y_0 such that l=bx0+cy0l = bx_0 + cy_0 is the least positive integer in AA. Now we want to show that l=gl = g.

  1. Seeking a contradiction, assume lbl \nmid b. Then the division algorithm gives that b=lq+rb = lq + r for some integers q,rq, r where 0<r<l0 \lt r \lt l.

    Then, r=blq=bq(bx0+cy0)=b(1qx0)+c(qy0)r = b - lq = b - q(bx_0 + cy_0) = b(1 - qx_0) + c(-qy_0). This results in rAr \in A and 0<r<l,0 \lt r \lt l, contradiction. The same argument applied on cc shows that lcl \vert c.

  2. Let g=(b,c)g = (b, c), then there exists integers B,CB, C such that gB=bgB = b and gC=cgC = c. So l=bx0+cy0=g(Bx0+Cy0)l = bx_0 + cy_0 = g(Bx_0 + Cy_0), so glg \vert l. Then glg \leq l, lbl \vert b and lcl \vert c. Therefore, ll is the greatest common divisor.

Euclidean Algorithm

Given integers b,cb, c not both equal to zero, the Euclidean algorithm is defined as

b=cq1+r1r1(0c)c=r1q2+r2r2(0r1)r1=r2q3+r3r3(0r1)rj2=rj1qj+rjrj(0rj1)rj1=rjqj+1stop when rj+1=0.\begin{array}{l c l} b = cq_1 + r_1 & \quad & r_1 \in (0\dots c)\\ c = r_1q_2 + r_2 & & r_2 \in (0\dots r_1)\\ r_1 = r_2q_3 + r_3 & & r_3 \in (0\dots r_1)\\ & \vdots &\\ r_{j - 2} = r_{j-1}q_j + r_j &&r_j \in (0\dots r_{j-1})\\ r_{j - 1} = r_jq_{j + 1} & & \text{stop when } r_{j + 1} = 0. \end{array}

Then, the value rj=(b,c)r_j = (b, c).

§ Bézout Coefficients

The extended Euclidean algorithm can be used to find Bézout coefficients, i.e. in a given a,ba, b we can find coefficients s,ts, t for the linear Diophantine equation satisfying

sa+tb=(a,b).s\cdot a + t\cdot 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)bx_0 + cy_0=(b,c) by rewriting each rir_i in terms of bb and cc.

r1=bcq1r2=cr1q2=c(bcq1)q2rj=c(q1q2)y0+b(q2)x0\begin{align*} r_1 &= b - cq_1\\ r_2 &= c - r_1q_2\\ &= c - (b - cq_1)q_2\\ &\mkern.5em\vdots\\ r_j &= c\underbrace{(q_1\cdot q_2\cdot \dots)}_{y_0} + b\underbrace{(q_2 \cdot \dots)}_{x_0} \end{align*}

The values x0x_0 and y0y_0 are our Bézout coefficients.

§ Modular Inverse

Modular Inverse

Given aZna \in \mathbb{Z}_n and (a,n)=1(a, n) = 1, we say that bb is the modular inverse of aa mod nn iff

abn1.a\cdot b \equiv_n 1.

We also write b=a1b = a^{-1}. Keep in mind that we are in Zn\mathbb{Z}_n, therefore a1a^{-1} must also be an integer.

If we want to find the modular inverse of aa modulo nn, we can use the extended Euclidean algorithm on (a,n)(a, n). Then, the Bézout coefficient corresponding to aa is congruent to the modular inverse of aa.