Hubbry Logo
Extended Euclidean algorithmExtended Euclidean algorithmMain
Open search
Extended Euclidean algorithm
Community hub
Extended Euclidean algorithm
logo
7 pages, 0 posts
0 subscribers
Be the first to start a discussion here.
Be the first to start a discussion here.
Contribute something
Extended Euclidean algorithm
Extended Euclidean algorithm
from Wikipedia

In arithmetic and computer programming, the extended Euclidean algorithm is an extension to the Euclidean algorithm, and computes, in addition to the greatest common divisor (gcd) of integers a and b, also the coefficients of Bézout's identity, which are integers x and y such that

This is a certifying algorithm, because the gcd is the only number that can simultaneously satisfy this equation and divide the inputs.[1] 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

[edit]

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 of quotients and a sequence of remainders such that

It is the main property of Euclidean division that the inequalities on the right define uniquely and from and

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

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

The computation also stops when and gives

  • is the greatest common divisor of the input and
  • The Bézout coefficients are and that is
  • The quotients of a and b by their greatest common divisor are given by and

Moreover, if a and b are both positive and , then

for where 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 the algorithm can be done without integer overflow by a computer program using integers of a fixed size that is larger than that of a and b.

Example

[edit]

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.

index i quotient qi−1 Remainder ri si ti
0 240 1 0
1 46 0 1
2 240 ÷ 46 = 5 2405 × 46 = 10 15 × 0 = 1 0 − 5 × 1 = −5
3 46 ÷ 10 = 4 464 × 10 = 6 04 × 1 = −4 1 − 4 × −5 = 21
4 10 ÷ 6 = 1 101 × 6 = 4 11 × −4 = 5 −5 − 1 × 21 = −26
5 6 ÷ 4 = 1 61 × 4 = 2 −41 × 5 = −9 21 − 1 × −26 = 47
6 4 ÷ 2 = 2 42 × 2 = 0 52 × −9 = 23 −26 − 2 × 47 = −120

Proof

[edit]

As the sequence of the is a decreasing sequence of nonnegative integers (from i = 2 on). Thus it must stop with some This proves that the algorithm stops eventually.

As the greatest common divisor is the same for and This shows that the greatest common divisor of the input is the same as that of This proves that is the greatest common divisor of a and b. (Until this point, the proof is the same as that of the classical Euclidean algorithm.)

As and we have for i = 0 and 1. The relation follows by induction for all :

Thus and are Bézout coefficients.

Consider the matrix

The recurrence relation may be rewritten in matrix form

The matrix is the identity matrix and its determinant is one. The determinant of the rightmost matrix in the preceding formula is −1. It follows that the determinant of is In particular, for we have Viewing this as a Bézout's identity, this shows that and are coprime. The relation that has been proved above and Euclid's lemma show that divides b, that is that for some integer d. Dividing by the relation gives So, and 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 . Then, , and if , it can be seen that the s and t sequences for (a,b) under the EEA 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 assume that without loss of generality.

It can be seen that is 1 and (which exists by ) is a negative integer. Thereafter, the alternate in sign and strictly increase in magnitude, which follows inductively from the definitions and the fact that for , the case holds because . The same is true for the after the first few terms, for the same reason. Furthermore, it is easy to see that (when a and b are both positive and ). Thus, noticing that , we obtain

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

Polynomial extended Euclidean algorithm

[edit]

For univariate polynomials with coefficients in a field, everything works similarly, Euclidean division, Bézout's identity and extended Euclidean algorithm. The first difference is that, in the Euclidean division and the algorithm, the inequality has to be replaced by an inequality on the degrees Otherwise, everything which precedes in this article remains the same, simply by replacing integers by polynomials.

A second difference lies in the bound on the size of the Bézout coefficients provided by the extended Euclidean algorithm, which is more accurate in the polynomial case, leading to the following theorem.

If a and b are two nonzero polynomials, then the extended Euclidean algorithm produces the unique pair of polynomials (s, t) such that

and

A third difference is that, in the polynomial case, the greatest common divisor is defined only up to the multiplication by a nonzero constant. There are several ways to define unambiguously a greatest common divisor.

In mathematics, it is common to require that the greatest common divisor be a monic polynomial. To get this, it suffices to divide every element of the output by the leading coefficient of This allows that, if a and b are coprime, one gets 1 in the right-hand side of Bézout's inequality. Otherwise, one may get any nonzero constant. In computer algebra, the polynomials commonly have integer coefficients, and this way of normalizing the greatest common divisor introduces too many fractions to be convenient.

The second way to normalize the greatest common divisor in the case of polynomials with integer coefficients is to divide every output by the content of to get a primitive greatest common divisor. If the input polynomials are coprime, this normalisation also provides a greatest common divisor equal to 1. The drawback of this approach is that a lot of fractions should be computed and simplified during the computation.

A third approach consists in extending the algorithm of subresultant pseudo-remainder sequences in a way that is similar to the extension of the Euclidean algorithm to the extended Euclidean algorithm. This allows that, when starting with polynomials with integer coefficients, all polynomials that are computed have integer coefficients. Moreover, every computed remainder is a subresultant polynomial. In particular, if the input polynomials are coprime, then the Bézout's identity becomes

where denotes the resultant of a and b. In this form of Bézout's identity, there is no denominator in the formula. If one divides everything by the resultant one gets the classical Bézout's identity, with an explicit common denominator for the rational numbers that appear in it.

Pseudocode

[edit]

To implement the algorithm that is described above, one should first remark that only the two last values of the indexed variables are needed at each step. Thus, for saving memory, each indexed variable must be replaced by just two variables.

For simplicity, the following algorithm (and the other algorithms in this article) uses parallel assignments. In a programming language which does not have this feature, the parallel assignments need to be simulated with an auxiliary variable. For example, the first one,

(old_r, r) := (r, old_r - quotient × r)

is equivalent to

prov := r;
r := old_r - quotient × prov;
old_r := prov;

and similarly for the other parallel assignments. This leads to the following code:

function extended_gcd(a, b)
    (old_r, r) := (a, b)
    (old_s, s) := (1, 0)
    (old_t, t) := (0, 1)
    
    while r ≠ 0 do
        quotient := old_r div r
        (old_r, r) := (r, old_r − quotient × r)
        (old_s, s) := (s, old_s − quotient × s)
        (old_t, t) := (t, old_t − quotient × t)
    
    output "Bézout coefficients:", (old_s, old_t)
    output "greatest common divisor:", old_r
    output "quotients by the gcd:", (t, s)

The quotients of a and b by their greatest common divisor, which is output, may have an incorrect sign. This is easy to correct at the end of the computation but has not been done here for simplifying the code. Similarly, if either a or b is zero and the other is negative, the greatest common divisor that is output is negative, and all the signs of the output must be changed.

Finally, notice that in Bézout's identity, , one can solve for given . Thus, an optimization to the above algorithm is to compute only the sequence (which yields the Bézout coefficient ), and then compute at the end:

function extended_gcd(a, b)
    s := 0;    old_s := 1
    r := b;    old_r := a
         
    while r ≠ 0 do
        quotient := old_r div r
        (old_r, r) := (r, old_r − quotient × r)
        (old_s, s) := (s, old_s − quotient × s)
    
    if b ≠ 0 then
        bezout_t := (old_r − old_s × a) div b
    else
        bezout_t := 0
    
    output "Bézout coefficients:", (old_s, bezout_t)
    output "greatest common divisor:", old_r

However, in many cases this is not really an optimization: whereas the former algorithm is not susceptible to overflow when used with machine integers (that is, integers with a fixed upper bound of digits), the multiplication of old_s × a in computation of bezout_t can overflow, limiting this optimization to inputs which can be represented in less than half the maximal size. When using integers of unbounded size, the time needed for multiplication and division grows quadratically with the size of the integers. This implies that the "optimisation" replaces a sequence of multiplications/divisions of small integers by a single multiplication/division, which requires more computing time than the operations that it replaces, taken together.

Simplification of fractions

[edit]

A fraction a/b is in canonical simplified form if a and b are coprime and b is positive. This canonical simplified form can be obtained by replacing the three output lines of the preceding pseudo code by

if s = 0 then output "Division by zero"
if s < 0 then s := −s;  t := −t   (for avoiding negative denominators)
if s = 1 then output t        (for avoiding denominators equal to 1)
output t/s

The proof of this algorithm relies on the fact that s and t are two coprime integers such that as + bt = 0, and thus . To get the canonical simplified form, it suffices to move the minus sign for having a positive denominator.

If b divides a evenly, the algorithm executes only one iteration, and we have s = 1 at the end of the algorithm. It is the only case where the output is an integer.

Computing multiplicative inverses in modular structures

[edit]

The extended Euclidean algorithm is the essential tool for computing multiplicative inverses in modular structures, typically the modular integers and the algebraic field extensions. A notable instance of the latter case are the finite fields of non-prime order.

Modular integers

[edit]

If n is a positive integer, the ring Z/nZ may be identified with the set {0, 1, ..., n-1} of the remainders of Euclidean division by n, the addition and the multiplication consisting in taking the remainder by n of the result of the addition and the multiplication of integers. An element a of Z/nZ has a multiplicative inverse (that is, it is a unit) if it is coprime to n. In particular, if n is prime, a has a multiplicative inverse if it is not zero (modulo n). Thus Z/nZ is a field if and only if n is prime.

Bézout's identity asserts that a and n are coprime if and only if there exist integers s and t such that

Reducing this identity modulo n gives

Thus t, or, more exactly, the remainder of the division of t by n, is the multiplicative inverse of a modulo n.

To adapt the extended Euclidean algorithm to this problem, one should remark that the Bézout coefficient of n is not needed, and thus does not need to be computed. Also, for getting a result which is positive and lower than n, one may use the fact that the integer t provided by the algorithm satisfies |t| < n. That is, if t < 0, one must add n to it at the end. This results in the pseudocode, in which the input n is an integer larger than 1.

function inverse(a, n)
    t := 0;     newt := 1
    r := n;     newr := a

    while newr ≠ 0 do
        quotient := r div newr
        (t, newt) := (newt, t − quotient × newt) 
        (r, newr) := (newr, r − quotient × newr)

    if r > 1 then
        return "a is not invertible"
    if t < 0 then
        t := t + n

    return t

Simple algebraic field extensions

[edit]

The extended Euclidean algorithm is also the main tool for computing multiplicative inverses in simple algebraic field extensions. An important case, widely used in cryptography and coding theory, is that of finite fields of non-prime order. In fact, if p is a prime number, and q = pd, the field of order q is a simple algebraic extension of the prime field of p elements, generated by a root of an irreducible polynomial of degree d.

A simple algebraic extension L of a field K, generated by the root of an irreducible polynomial p of degree d may be identified to the quotient ring , and its elements are in bijective correspondence with the polynomials of degree less than d. The addition in L is the addition of polynomials. The multiplication in L is the remainder of the Euclidean division by p of the product of polynomials. Thus, to complete the arithmetic in L, it remains only to define how to compute multiplicative inverses. This is done by the extended Euclidean algorithm.

The algorithm is very similar to that provided above for computing the modular multiplicative inverse. There are two main differences: firstly the last but one line is not needed, because the Bézout coefficient that is provided always has a degree less than d. Secondly, the greatest common divisor which is provided, when the input polynomials are coprime, may be any nonzero element of K; this Bézout coefficient (a polynomial generally of positive degree) has thus to be multiplied by the inverse of this element of K. In the pseudocode which follows, p is a polynomial of degree greater than one, and a is a polynomial.

function inverse(a, p)
    t := 0;     newt := 1
    r := p;     newr := a

    while newr ≠ 0 do
        quotient := r div newr
        (r, newr) := (newr, r − quotient × newr)
        (t, newt) := (newt, t − quotient × newt)

    if degree(r) > 0 then 
        return "Either p is not irreducible or a is a multiple of p"

    return (1/r) × t

Example

[edit]

For example, if the polynomial used to define the finite field GF(28) is p = x8 + x4 + x3 + x + 1, and a = x6 + x4 + x + 1 is the element whose inverse is desired, then performing the algorithm results in the computation described in the following table. Let us recall that in fields of order 2n, one has −z = z and z + z = 0 for every element z in the field). Since 1 is the only nonzero element of GF(2), the adjustment in the last line of the pseudocode is not needed.

step quotient r, newr s, news t, newt
p = x8 + x4 + x3 + x + 1 1 0
a = x6 + x4 + x + 1 0 1
1 x2 + 1 x2 = pa (x2 + 1) 1 x2 + 1 = 0 − 1 · (x2 + 1)
2 x4 + x2 x + 1 = ax2 (x4 + x2) x4+x2 = 0 − 1(x4+x2) x6 + x2 + 1 = 1 − (x4 + x2) (x2 + 1)
3 x + 1 1 = x2 − (x + 1) (x + 1) x5+x4+x3+x2+1 = 1 − (x +1)(x4 + x2) x7 + x6 + x3 + x = (x2 + 1) − (x + 1) (x6 + x2 + 1)
4 x + 1 0 = (x + 1) − 1 × (x + 1) x6 + x4 + x + 1 = (x4+x2) − (x+1)(x5+x4+x3+x2+1)

Thus, the inverse is x7 + x6 + x3 + x, as can be confirmed by multiplying the two elements together, and taking the remainder by p of the result.

The case of more than two numbers

[edit]

One can handle the case of more than two numbers iteratively. First we show that . To prove this let . By definition of gcd is a divisor of and . Thus for some . Similarly is a divisor of so for some . Let . By our construction of , but since is the greatest divisor is a unit. And since the result is proven.

So if then there are and such that so the final equation will be

So then to apply to n numbers we use induction

with the equations following directly.

See also

[edit]

References

[edit]
[edit]
Revisions and contributorsEdit on WikipediaRead on Wikipedia
from Grokipedia
The Extended Euclidean algorithm is an efficient computational procedure in that extends the classical to not only determine the (GCD) of two integers a and b, denoted d = gcd(a, b), but also to find integers x and y such that a x + b y = d. This expresses d as a Bézout combination of a and b, proving , which states that such coefficients always exist for any pair of integers. The operates by iteratively applying the division algorithm, tracking quotients and while maintaining expressions for each as a of the original inputs. It begins with r0 = a = 1·a + 0·b and r1 = b = 0·a + 1·b, then computes subsequent ri = ri-2 - qi · ri-1, updating the coefficients accordingly until a of zero is reached, at which point the penultimate is the GCD and its coefficients provide the solution. The process runs in O(log min(a, b)) , making it highly practical for large integers. Originating as an enhancement to Euclid's algorithm from Elements (circa 300 BCE), the extended version explicitly constructs the coefficients implied by the original method, with the theorem formalized as by Étienne Bézout in 1779, though the core idea predates him. Key applications include solving linear Diophantine equations a x + b y = c (feasible when d divides c), computing modular multiplicative inverses when gcd(a, m) = 1 (vital for systems like RSA encryption), and facilitating primality testing and in . These uses underscore its foundational role in modern , , and algebraic structures like rings.

Fundamentals

Definition and Description

The extended Euclidean algorithm builds upon the classical Euclidean algorithm, originally described by the ancient Greek mathematician Euclid in the 3rd century BCE for computing the greatest common divisor (GCD) of two integers. Early extensions of this method to find integer coefficients expressing the GCD as a linear combination trace back to the Indian mathematician Aryabhata around 500 CE, who developed a procedure known as the "pulverizer" (kuttaka) for solving linear Diophantine equations. In the 18th century, French mathematician Étienne Bézout formalized the underlying identity in his 1779 work Théorie générale des équations algébriques, establishing it for both integers and polynomials, though the algorithmic back-substitution process predates this attribution. The algorithm's significance in modern computational number theory emerged in the 20th century, where it became essential for efficient implementations in areas like cryptography and algebraic computation. Formally, given two integers aa and bb with a>b0a > b \geq 0 and not both zero, the extended Euclidean algorithm computes d=gcd(a,b)d = \gcd(a, b) along with integers xx and yy satisfying Bézout's identity: ax+by=d.ax + by = d. This equation guarantees that the GCD can always be expressed as an integer of aa and bb, reflecting the ideal generated by aa and bb in the . The algorithm proceeds at a high level by iteratively applying the division step of the —replacing (a,b)(a, b) with (b,r)(b, r) where r=aqbr = a - q b for qq, until the remainder is zero—while tracking coefficients through back-substitution to reconstruct the for the final non-zero , which is dd. The Bézout coefficients xx and yy are not unique; if (x,y)(x', y') is another pair satisfying the equation, then x=x+k(b/d)x' = x + k (b/d) and y=yk(a/d)y' = y - k (a/d) for some kk, making the solutions unique up to multiples of (b/d,a/d)(b/d, -a/d). This modulo structure arises directly from the equation's homogeneity and the properties of the GCD.

Illustrative Example

To illustrate the extended Euclidean algorithm, consider the problem of computing gcd(240,46)\gcd(240, 46) and finding integers xx and yy such that 240x+46y=gcd(240,46)240x + 46y = \gcd(240, 46). This process follows , which guarantees the existence of such integers for any pair of integers. The algorithm proceeds in two phases: first, applying the to find the gcd via successive divisions, tracking quotients and remainders; second, back-substituting to express the gcd as a . The following table summarizes the forward phase, where each remainder rir_i is computed as ri=ri2qiri1r_{i} = r_{i-2} - q_i r_{i-1}, with initial values r0=240r_0 = 240 and r1=46r_1 = 46.
Steprir_i (remainder)Quotient qiq_iExpression for rir_i
0240-240=1240+046240 = 1 \cdot 240 + 0 \cdot 46
146-46=0240+14646 = 0 \cdot 240 + 1 \cdot 46
210510=24054610 = 240 - 5 \cdot 46
3646=464106 = 46 - 4 \cdot 10
4414=10164 = 10 - 1 \cdot 6
5212=6142 = 6 - 1 \cdot 4
6020=4220 = 4 - 2 \cdot 2
The process terminates when the remainder is 0, so gcd(240,46)=2\gcd(240, 46) = 2. In the back-substitution phase, express the gcd using the previous equations, starting from the penultimate step and substituting upward: 2=614=61(1016)(substitute 4=1016)=26110=2(46410)110(substitute 6=46410)=246910=2469(240546)(substitute 10=240546)=9240+4746.\begin{align*} 2 &= 6 - 1 \cdot 4 \\ &= 6 - 1 \cdot (10 - 1 \cdot 6) \quad (\text{substitute } 4 = 10 - 1 \cdot 6) \\ &= 2 \cdot 6 - 1 \cdot 10 \\ &= 2 \cdot (46 - 4 \cdot 10) - 1 \cdot 10 \quad (\text{substitute } 6 = 46 - 4 \cdot 10) \\ &= 2 \cdot 46 - 9 \cdot 10 \\ &= 2 \cdot 46 - 9 \cdot (240 - 5 \cdot 46) \quad (\text{substitute } 10 = 240 - 5 \cdot 46) \\ &= -9 \cdot 240 + 47 \cdot 46. \end{align*} Thus, x=9x = -9 and y=47y = 47, satisfying 240(9)+46(47)=2240(-9) + 46(47) = 2. To verify, compute 240×(9)=2160240 \times (-9) = -2160 and 46×47=216246 \times 47 = 2162, so 2160+2162=2-2160 + 2162 = 2. This confirms the result. Common pitfalls include misidentifying the gcd when the remainder reaches zero—the gcd is always the last nonzero remainder—and overlooking that coefficients like xx and yy may be negative, which is valid under Bézout's identity, though equivalent nonnegative pairs can be obtained by adding multiples of 46/2=2346/2 = 23 to xx and subtracting multiples of 240/2=120240/2 = 120 from yy.

Mathematical Proof

The correctness of the extended Euclidean algorithm, which computes integers xx and yy such that ax+by=gcd(a,b)ax + by = \gcd(a, b) for nonnegative integers ab0a \geq b \geq 0, can be established by on bb, leveraging that gcd(a,b)\gcd(a, b) is the smallest positive of aa and bb. Base case: If b=0b = 0, then gcd(a,0)=a\gcd(a, 0) = a, and the algorithm returns x=1x = 1, y=0y = 0, satisfying a1+00=aa \cdot 1 + 0 \cdot 0 = a. This holds since any integer divides zero, and the greatest of aa and zero is aa itself. Inductive step: Assume the algorithm is correct for all pairs with second argument less than b>0b > 0; that is, for the recursive call on gcd(b,r)\gcd(b, r) where r=amodb=aqbr = a \mod b = a - qb and q=a/bq = \lfloor a/b \rfloor, it returns integers xx' and yy' such that bx+ry=gcd(b,r)=d=gcd(a,b)b x' + r y' = \gcd(b, r) = d = \gcd(a, b). Substituting r=aqbr = a - qb yields bx+(aqb)y=db x' + (a - qb) y' = d, or ay+b(xqy)=da y' + b (x' - q y') = d. Thus, setting x=yx = y' and y=xqyy = x' - q y' satisfies ax+by=da x + b y = d, confirming the algorithm's output for the original pair. The algorithm terminates because the underlying does: each remainder strictly decreases (r<br < b) and remains nonnegative, eventually reaching zero after finitely many steps, at most O(logb)O(\log b) iterations by properties of the Fibonacci sequence bounding the worst case. The solution (x,y)(x, y) is not unique; all integer solutions to ax+by=da x + b y = d are given by x=x0+k(b/d)x = x_0 + k (b/d) and y=y0k(a/d)y = y_0 - k (a/d) for integer kk, where (x0,y0)(x_0, y_0) is any particular solution and d=gcd(a,b)d = \gcd(a, b), since adding multiples of b/db/d to xx and subtracting multiples of a/da/d from yy preserves the equation due to a(b/d)=b(a/d)a (b/d) = b (a/d).

Algorithmic Implementation

Recursive Formulation

The recursive formulation of the extended Euclidean algorithm computes the greatest common divisor d=gcd(a,b)d = \gcd(a, b) of two integers aa and bb (assuming ab0a \geq b \geq 0), along with the Bézout coefficients xx and yy satisfying the equation ax+by=dax + by = d. This version naturally extends the recursive structure of the standard Euclidean algorithm by tracking the coefficients through back-substitution in the recursion. The function, typically denoted as extended_gcd(a, b), returns a tuple (d,x,y)(d, x, y). In the base case, if b=0b = 0, it returns (a,1,0)(a, 1, 0), since gcd(a,0)=a\gcd(a, 0) = a and a1+00=aa \cdot 1 + 0 \cdot 0 = a. For the recursive case, the algorithm calls itself on the pair (b,amodb)(b, a \mod b) to obtain (d,x1,y1)(d, x_1, y_1) where d=gcd(b,amodb)d = \gcd(b, a \mod b) and bx1+(amodb)y1=db x_1 + (a \mod b) y_1 = d. It then computes the coefficients for the original pair as x=y1x = y_1 and y=x1aby1y = x_1 - \left\lfloor \frac{a}{b} \right\rfloor y_1, before returning (d,x,y)(d, x, y). This adjustment ensures the linear combination holds for aa and bb, mirroring the division step in the . The following pseudocode illustrates this structure:

function extended_gcd(a, b): if b == 0: return (a, 1, 0) else: (d, x1, y1) = extended_gcd(b, a % b) x = y1 y = x1 - (a // b) * y1 return (d, x, y)

function extended_gcd(a, b): if b == 0: return (a, 1, 0) else: (d, x1, y1) = extended_gcd(b, a % b) x = y1 y = x1 - (a // b) * y1 return (d, x, y)

The time complexity of this recursive formulation is O(logmin(a,b))O(\log \min(a, b)) arithmetic operations, matching that of the standard Euclidean algorithm, as the number of recursive calls equals the number of division steps until the base case is reached, and each call performs constant work beyond the recursion. The recursion depth is also O(logmin(a,b))O(\log \min(a, b)), since the arguments decrease rapidly. This recursive approach offers advantages in conceptual clarity, as its structure directly parallels the inductive proof of the 's correctness, making it particularly suitable for understanding and verifying the computation of Bézout coefficients through mathematical induction on the input size.

Iterative Pseudocode

The iterative formulation of the extended computes the greatest common divisor d=gcd(a,b)d = \gcd(a, b) along with Bézout coefficients xx and yy such that ax+by=dax + by = d, using a loop to simulate the divisions and back-substitutions without recursion. This approach is particularly useful in programming environments where recursion depth limits could be exceeded for large inputs. The following language-agnostic pseudocode illustrates the core structure, initializing coefficients for the initial remainders and updating them in each iteration while shifting the values of aa and bb:

function extended_gcd(a, b): if b == 0: return a, 1, 0 prevx, x = 1, 0 prevy, y = 0, 1 while b != 0: q = a // b a, b = b, a % b x, prevx = prevx - q * x, x y, prevy = prevy - q * y, y return a, prevx, prevy // returns d, x, y where original_a * x + original_b * y = d

function extended_gcd(a, b): if b == 0: return a, 1, 0 prevx, x = 1, 0 prevy, y = 0, 1 while b != 0: q = a // b a, b = b, a % b x, prevx = prevx - q * x, x y, prevy = prevy - q * y, y return a, prevx, prevy // returns d, x, y where original_a * x + original_b * y = d

In this implementation, the variables track the coefficients for the current and previous remainders, ensuring the invariant holds after each update. An array-based variant builds sequences for the remainders rr, coefficients ss, and coefficients tt iteratively, akin to constructing a table of values. Initialize r0=ar_0 = a, r1=br_1 = b, s0=1s_0 = 1, s1=0s_1 = 0, t0=0t_0 = 0, t1=1t_1 = 1. Then, for each subsequent index i2i \geq 2, compute the quotient qi=ri2/ri1q_i = \lfloor r_{i-2} / r_{i-1} \rfloor, and update ri=ri2qiri1r_i = r_{i-2} - q_i r_{i-1}, si=si2qisi1s_i = s_{i-2} - q_i s_{i-1}, ti=ti2qiti1t_i = t_{i-2} - q_i t_{i-1}. Continue until ri=0r_i = 0; the GCD is the last non-zero remainder ri1r_{i-1}, with corresponding si1s_{i-1} and ti1t_{i-1}. This method explicitly stores all intermediate coefficients, facilitating verification or further computations. To handle edge cases, if a=0a = 0 and b0b \neq 0, return d=bd = |b|, x=0x = 0, y=sign(b)y = \operatorname{sign}(b); if b=0b = 0, return d=ad = |a|, x=sign(a)x = \operatorname{sign}(a), y=0y = 0. For negative inputs, apply the algorithm to a|a| and b|b| and adjust the signs of the coefficients based on the original signs to ensure ax+by=d>0a x + b y = d > 0. The iterative versions achieve the same time complexity as the underlying , O(logmin(a,b))O(\log \min(|a|, |b|)), due to the logarithmic number of divisions, while avoiding overhead and potential stack issues.

Generalizations

Polynomial Version

The extended Euclidean algorithm generalizes to univariate polynomials over a field KK, such as the rational numbers or real numbers, in the KK. Given two polynomials f,gKf, g \in K, not both zero, the algorithm computes a monic dKd \in K and polynomials s,tKs, t \in K satisfying fs+gt=df s + g t = d. This adaptation relies on the fact that KK forms a , where the degree function serves as the Euclidean norm, enabling division with remainder analogous to the integer case. The algorithm proceeds recursively, mirroring the structure for integers but using polynomial division. , assume deg(f)deg(g)\deg(f) \geq \deg(g). Divide ff by gg to obtain the qq and rr such that f=qg+rf = q g + r with deg(r)<deg(g)\deg(r) < \deg(g). Recursively apply the algorithm to gg and rr, yielding polynomials ss' and tt' where gs+rt=dg s' + r t' = d. Back-substituting the expression for rr gives fs+g(tqs)=df s' + g (t' - q s') = d, so set s=ss = s' and t=tqst = t' - q s'. The base cases occur when g=0g = 0, in which d=fd = f (normalized to be monic) with s=1s = 1 and t=0t = 0, or when gg is a nonzero constant, in which d=1d = 1 (the monic unit) and the coefficients are scaled accordingly by the leading coefficient of gg. Normalization ensures dd is monic by dividing by its leading coefficient throughout. For illustration, consider f(x)=x2+1f(x) = x^2 + 1 and g(x)=x+1g(x) = x + 1 over the real numbers R\mathbb{R}. Dividing ff by gg yields quotient x1x - 1 and constant remainder 22. The recursion on gg and 22 produces the monic gcd d=1d = 1, and back-substitution provides explicit ss and tt satisfying the identity, though the full computation involves scaling by the leading coefficient of the remainder. The computational complexity of this algorithm is O(deg(f)deg(g))O(\deg(f) \cdot \deg(g)) arithmetic operations in the field KK, assuming standard polynomial division algorithms. With faster multiplication techniques, such as those based on fast Fourier transforms, the complexity can improve, but the basic version aligns with the quadratic scaling in degrees.

Extension to Multiple Arguments

The extended Euclidean algorithm can be generalized to compute the greatest common divisor dd of n>2n > 2 integers a1,a2,,ana_1, a_2, \dots, a_n and express dd as an integer d=i=1nxiaid = \sum_{i=1}^n x_i a_i, where the coefficients xix_i are integers (not necessarily unique). This generalization relies on Bézout's identity extended to multiple integers, which holds by induction: the base case for two integers is given by the standard extended Euclidean algorithm, and for additional integers, the result follows from expressing the GCD of the first n1n-1 as a linear combination and then incorporating the nnth via another application of the algorithm. The method proceeds iteratively by pairwise application. Begin by applying the extended Euclidean algorithm to the first two integers a1a_1 and a2a_2 to obtain d2=gcd(a1,a2)=x1a1+x2a2d_2 = \gcd(a_1, a_2) = x_1 a_1 + x_2 a_2. Then, compute d3=gcd(d2,a3)=y1d2+y3a3d_3 = \gcd(d_2, a_3) = y_1 d_2 + y_3 a_3 using the extended Euclidean algorithm again, and substitute the expression for d2d_2 to yield d3=(y1x1)a1+(y1x2)a2+y3a3d_3 = (y_1 x_1) a_1 + (y_1 x_2) a_2 + y_3 a_3. Continue this process sequentially for each subsequent aia_i, updating the coefficients for prior terms by multiplication with the new intermediate coefficient and adding the new term's coefficient. This yields the full after n1n-1 applications. For example, consider the integers 48, 18, and 30. First, apply the to 48 and 18: 48=218+12,18=112+6,12=26+0.\begin{align*} 48 &= 2 \cdot 18 + 12, \\ 18 &= 1 \cdot 12 + 6, \\ 12 &= 2 \cdot 6 + 0. \end{align*} Back-substituting gives gcd(48,18)=6=148+318\gcd(48, 18) = 6 = -1 \cdot 48 + 3 \cdot 18. Now incorporate 30 by computing gcd(6,30)\gcd(6, 30): 30=56+0.\begin{align*} 30 &= 5 \cdot 6 + 0. \end{align*} Thus, 6=16+0306 = 1 \cdot 6 + 0 \cdot 30. Substituting the prior expression yields 6=148+318+0306 = -1 \cdot 48 + 3 \cdot 18 + 0 \cdot 30. The coefficients are updated accordingly, confirming d=6d = 6. In this iterative approach, the coefficients xix_i can grow rapidly—potentially exponentially in nn—due to successive multiplications during substitution, especially when intermediate quotients are large, although minimal coefficients satisfying the equation are bounded by the sum of the ai|a_i|. This growth makes the method less efficient for large nn, as the bit length of coefficients may increase significantly. If only the GCD is needed without coefficients, an alternative is to use Stein's algorithm (also known as the ) iteratively in a pairwise fashion, which avoids divisions and uses bit shifts and subtractions for faster computation on large integers, though it does not directly provide Bézout coefficients.

Applications

Rational Number Simplification

To simplify a expressed as a pq\frac{p}{q} where pp and qq are integers and q0q \neq 0, the extended Euclidean algorithm (EEA) is applied to compute the d=gcd(p,q)d = \gcd(|p|, |q|). The simplified form is then p/dq/d\frac{p/d}{q/d}, with signs adjusted to ensure the denominator is positive (e.g., if q<0q < 0, multiply numerator and denominator by -1). This process reduces the to its lowest terms by dividing both numerator and denominator by their common divisor dd. The EEA is particularly efficient for this task because it computes the GCD in O(logmin(p,q))O(\log \min(|p|, |q|)) steps using repeated division, outperforming naive factorization methods for large integers. Although the EEA also outputs Bézout coefficients expressing dd as an integer linear combination of p|p| and q|q|, these are incidental for simplification and not required. For example, consider the fraction 24046\frac{240}{46}. Applying the EEA yields gcd(240,46)=2\gcd(240, 46) = 2, so the simplified form is 240/246/2=12023\frac{240/2}{46/2} = \frac{120}{23}. Edge cases must be handled carefully: if q=0q = 0, the fraction is undefined; if p=0p = 0, it simplifies to 01\frac{0}{1}. The steps of the EEA on pp and qq directly mirror the continued fraction expansion of pq\frac{p}{q}, where the quotients from successive divisions form the continued fraction coefficients, terminating finitely for rationals. Historically, the EEA's precursor—the Euclidean algorithm for GCD—has been used since Euclid's Elements (Book VII, c. 300 BCE) in early arithmetic for handling exact fractions in rational computations.

Modular Multiplicative Inverses

In modular arithmetic, an integer aa has a multiplicative inverse modulo mm if and only if gcd(a,m)=1\gcd(a, m) = 1, meaning there exists an integer xx such that ax1(modm)a x \equiv 1 \pmod{m}. The extended Euclidean algorithm computes this inverse by finding integers xx and yy satisfying Bézout's identity ax+my=1a x + m y = 1, from which xmodmx \mod m (adjusted to a positive representative between 0 and m1m-1) serves as the inverse. To compute the inverse, apply the extended Euclidean algorithm to aa and mm:
  1. Perform the standard Euclidean algorithm to find gcd(a,m)\gcd(a, m); if it is not 1, no inverse exists.
  2. Use the extended version to back-substitute and express 1 as a linear combination ax+my=1a x + m y = 1.
  3. The coefficient xx is the inverse, taken modulo mm.
For example, to find the inverse of 5 17: 17=35+2,5=22+1,2=21+0.\begin{align*} 17 &= 3 \cdot 5 + 2, \\ 5 &= 2 \cdot 2 + 1, \\ 2 &= 2 \cdot 1 + 0. \end{align*} Back-substituting: 1=522,2=1735,1 = 5 - 2 \cdot 2, \quad 2 = 17 - 3 \cdot 5, 1=52(1735)=75217.1 = 5 - 2 \cdot (17 - 3 \cdot 5) = 7 \cdot 5 - 2 \cdot 17. Thus, 571(mod17)5 \cdot 7 \equiv 1 \pmod{17}, so the inverse is 7. If gcd(a,m)=d>1\gcd(a, m) = d > 1, no multiplicative inverse exists for aa modulo mm when solving ax1(modm)a x \equiv 1 \pmod{m}, as dd does not divide 1; more generally, the linear congruence axb(modm)a x \equiv b \pmod{m} is solvable if and only if dd divides bb. The extended Euclidean algorithm's efficiency in computing these inverses is crucial in , such as RSA decryption, where the private exponent dd is the modular inverse of the public exponent ee modulo ϕ(n)\phi(n), computed via the algorithm during . Similarly, in Diffie-Hellman variants like implicitly-certified public keys, inverses modulo p1p-1 are required to derive private keys from shared parameters. Wilson's theorem connects to modular inverses by stating that for a prime pp, (p1)!1(modp)(p-1)! \equiv -1 \pmod{p}, reflecting the structure of the modulo pp where every non-zero element has a unique inverse.

Inverses in Algebraic Extensions

In algebraic extensions of a field KK, such as simple extensions K[α]K[\alpha] where α\alpha is a of an irreducible polynomial m(x)Km(x) \in K, elements can be represented as polynomials in α\alpha of degree less than the degree of m(x)m(x). The extended Euclidean algorithm applied to polynomials over KK enables the computation of multiplicative inverses for elements that are coprime to m(x)m(x). To invert an element βK[α]\beta \in K[\alpha], represent β\beta as a polynomial β(x)K\beta(x) \in K with deg(β(x))<deg(m(x))\deg(\beta(x)) < \deg(m(x)). Apply the extended Euclidean algorithm to β(x)\beta(x) and m(x)m(x), which computes the and Bézout coefficients s(x),t(x)Ks(x), t(x) \in K such that s(x)β(x)+t(x)m(x)=gcd(β(x),m(x))s(x) \beta(x) + t(x) m(x) = \gcd(\beta(x), m(x)). Since m(x)m(x) is irreducible and β\beta is invertible if gcd(β(x),m(x))=1\gcd(\beta(x), m(x)) = 1 (a constant in KK), the coefficients yield s(x)β(x)1(modm(x))s(x) \beta(x) \equiv 1 \pmod{m(x)}, so the inverse is s(α)s(\alpha) reduced m(x)m(x). This process leverages the division algorithm in KK to generate the remainder sequence until the gcd is reached, then back-substitutes to express the gcd. For example, consider the quadratic extension Q(2)\mathbb{Q}(\sqrt{2})
Add your contribution
Related Hubs
Contribute something
User Avatar
No comments yet.