Lably
Skip to content

Extended Euclidean algorithm

Method for computing the relation of two integers with their greatest common divisor

  • 14 min read
  • 6 chapters

Related on Lably

Through Arithmetic, Computer programming and Greatest common divisor

This is a certifying algorithm, because the gcd is the only number that can simultaneously satisfy this equation and divide the inputs. It allows one to compute also, with almost no extra cost, the quotients of a and b by their greatest common divisor.

Extended Euclidean algorithm also refers to a very similar algorithm for computing the polynomial greatest common divisor and the coefficients of Bézout's identity of two univariate polynomials.

The extended Euclidean algorithm is particularly useful when a and b are coprime. With that provision, x is the modular multiplicative inverse of a modulo b, and y is the modular multiplicative inverse of b modulo a. Similarly, the polynomial extended Euclidean algorithm allows one to compute the multiplicative inverse in algebraic field extensions and, in particular in finite fields of non-prime order. It follows that both extended Euclidean algorithms are widely used in cryptography. In particular, the computation of the modular multiplicative inverse is an essential step in the derivation of key-pairs in the RSA public-key encryption method.

Description

The standard Euclidean algorithm proceeds by a succession of Euclidean divisions whose quotients are not used. Only the remainders are kept. For the extended algorithm, the successive quotients are used. More precisely, the standard Euclidean algorithm with a and b as input, consists of computing a sequence q1,…,qk of quotients and a sequence r0,…,rk+1 of remainders such that

r0=ar1=b⋮ri+1=ri−1−qiriand0≤ri+1<|ri|(this defines qi)⋮

It is the main property of Euclidean division that the inequalities on the right define uniquely qi and ri+1 from ri−1 and ri.

The computation stops when one reaches a remainder rk+1 which is zero; the greatest common divisor is then the last nonzero remainder rk.

The extended Euclidean algorithm proceeds similarly, but adds two other sequences, as follows

r0=ar1=bs0=1s1=0t0=0t1=1⋮⋮ri+1=ri−1−qiriand 0≤ri+1<|ri|(this defines qi)si+1=si−1−qisiti+1=ti−1−qiti⋮

The computation also stops when rk+1=0 and gives

  • rk is the greatest common divisor of the input a=r0 and b=r1.
  • The Bézout coefficients are sk and tk, that is gcd(a,b)=rk=ask+btk
  • The quotients of a and b by their greatest common divisor are given by sk+1=±bgcd(a,b) and tk+1=∓agcd(a,b) (the sign ∓ is opposite to ±).

Moreover, if a and b are both positive and gcd(a,b)≠min(a,b), then

|si|≤⌊b2gcd(a,b)⌋and|ti|≤⌊a2gcd(a,b)⌋

for 0≤i≤k, where ⌊x⌋ denotes the integral part of x, that is the greatest integer not greater than x.

This implies that the pair of Bézout's coefficients provided by the extended Euclidean algorithm is the minimal pair of Bézout coefficients, as being the unique pair satisfying both above inequalities.

It also means that if a and b fit into an unsigned integer data type, a computer program can compute the Bézout coefficients in the corresponding signed integer type without integer overflow.

Examples

The following table shows how the extended Euclidean algorithm proceeds with input 240 and 46. The greatest common divisor is the last nonzero entry, 2 in the column "remainder". The computation stops at row 6, because the remainder in it is 0. Bézout coefficients appear in the last two columns of the second-to-last row. In fact, it is easy to verify that −9 × 240 + 47 × 46 = 2. Finally the last two entries 23 and −120 of the last row are, up to the sign, the quotients of the input 46 and 240 by the greatest common divisor 2.

The following example uses a more compact notation. The greatest common divisor of a=68 and b=30 can be calculated like this:

g=gcd(68,30)↑−−(−2)=gcd(8,30)(−3)−−↑ =gcd(8,6)↑−−(−1)=gcd(2,6)(−3)−−↑=gcd(2,0)=2,

where ↑−−(−2) in the first step means that −2 times 30 is added to 68 (the gcd does not change when adding a multiple of one number to the other).

Applying the green-indicated additions of multiples analogously to equations, starting with a=1a+0b and b=0a+1b, leads to g=4a−9b, according to the adjacent calculations (the corresponding table far right uses row operations).

Proof

Since 0≤ri+1<|ri|, the sequence ri is a strictly decreasing sequence of non-negative integers, for i≥2. Thus, it must stop with some rk+1=0. This proves that the algorithm stops, eventually.

From the equation ri+1=ri−1−riqi it follows that gcd(ri−1,ri)=gcd(ri,ri+1). As a consequence, gcd(a,b)=gcd(r0,r1)=gcd(rk,rk+1)=gcd(rk,0)=rk. Until this point, the proof is the same as that of the classical Euclidean algorithm.

The recurrence relations, for i≥0,

{ri=asi+bti(−1)i=siti+1−tisi+1

can be proven by induction. In fact, for i=0 they reduce to

{r0=a=as0+bt01=(−1)0=1⋅1−0⋅0=s0t1−t0s1

Assuming they are satisfied for some i, the equations for i+1 follow from the computation

{ri+1=ri−1−riqi=(asi−1+bti−1)−(asi+bti)qi=(asi−1−asiqi)+(bti−1−btiqi)=asi+1+bti+1(−1)i+1=−(siti+1−tisi+1)=si+1(ti−qi+1ti+1)−ti+1(si−qi+1si+1)=si+1ti+2−ti+1si+2

In particular, the equation siti+1−tisi+1=(−1)i shows that sk+1 and tk+1 are coprime.

Multiplying 0=rk+1=ask+1+btk+1 by sk, it implies that

0=ask+1sk+btk+1sk=ask+1sk+b((−1)k+tksk+1)

Therefore, b=(−1)k+1(tk−ask)sk+1, from where it follows that sk+1 divides b. As a consequence, there is an integer d such that b=dsk+1. Dividing by sk+1 the relation ask+1+btk+1=0 gives a=−dtk+1. Hence, sk+1 and −tk+1 are coprime integers that are the quotients of a and b by a common factor, which is thus their greatest common divisor or its opposite.

To prove the last assertion, assume that a and b are both positive and gcd(a,b)≠min(a,b). Then, a≠b, and if a<b, it can be seen that the s and t sequences for (a,b) under the Extended Euclidean Algorithm are, up to initial 0s and 1s, the t and s sequences for (b,a). The definitions then show that the (a,b) case reduces to the (b,a) case. So,we can assume that a>b without loss of generality.

It can be seen that s2 is 1 and s3 (which exists by gcd(a,b)≠min(a,b)) is a negative integer. Thereafter, the si alternate in sign and strictly increase in magnitude, which follows inductively from the definitions and the fact that qi≥1 for 1≤i≤k, the case i=1 holds because a>b. The same is true for the ti after the first few terms, for the same reason. Furthermore, it is easy to see that qk≥2 (when a and b are both positive and gcd(a,b)≠min(a,b)). Thus, noticing that |sk+1|=|sk−1|+qk|sk|, we obtain |sk+1|=|bgcd(a,b)|≥2|sk|and|tk+1|=|agcd(a,b)|≥2|tk|.

This, accompanied by the fact that sk,tk are larger than or equal to in absolute value than any previous si or ti respectively completed the proof.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Number theory