Recent from talks
Contribute something
Nothing was collected or created yet.
Extended Euclidean algorithm
View on WikipediaIn 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 | 240 − 5 × 46 = 10 | 1 − 5 × 0 = 1 | 0 − 5 × 1 = −5 |
| 3 | 46 ÷ 10 = 4 | 46 − 4 × 10 = 6 | 0 − 4 × 1 = −4 | 1 − 4 × −5 = 21 |
| 4 | 10 ÷ 6 = 1 | 10 − 1 × 6 = 4 | 1 − 1 × −4 = 5 | −5 − 1 × 21 = −26 |
| 5 | 6 ÷ 4 = 1 | 6 − 1 × 4 = 2 | −4 − 1 × 5 = −9 | 21 − 1 × −26 = 47 |
| 6 | 4 ÷ 2 = 2 | 4 − 2 × 2 = 0 | 5 − 2 × −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 = p − a (x2 + 1) | 1 | x2 + 1 = 0 − 1 · (x2 + 1) |
| 2 | x4 + x2 | x + 1 = a − x2 (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]- ^ McConnell, Ross; Mehlhorn, Kurt; Näher, Stefan; Schweitzer, Pascal. "Certifying Algorithms" (PDF). Retrieved 29 September 2024.
- Knuth, Donald. The Art of Computer Programming. Addison-Wesley. Volume 2, Chapter 4.
- Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Pages 859–861 of section 31.2: Greatest common divisor.
External links
[edit]Extended Euclidean algorithm
View on GrokipediaFundamentals
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.[10] 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.[11] Formally, given two integers and with and not both zero, the extended Euclidean algorithm computes along with integers and satisfying Bézout's identity: This equation guarantees that the GCD can always be expressed as an integer linear combination of and , reflecting the ideal generated by and in the ring of integers.[10] The algorithm proceeds at a high level by iteratively applying the division step of the Euclidean algorithm—replacing with where for quotient , until the remainder is zero—while tracking coefficients through back-substitution to reconstruct the linear combination for the final non-zero remainder, which is .[12] The Bézout coefficients and are not unique; if is another pair satisfying the equation, then and for some integer , making the solutions unique up to multiples of .[13] This modulo structure arises directly from the equation's homogeneity and the properties of the GCD.[12]Illustrative Example
To illustrate the extended Euclidean algorithm, consider the problem of computing and finding integers and such that . This process follows Bézout's identity, which guarantees the existence of such integers for any pair of integers.[14] The algorithm proceeds in two phases: first, applying the Euclidean algorithm to find the gcd via successive divisions, tracking quotients and remainders; second, back-substituting to express the gcd as a linear combination. The following table summarizes the forward phase, where each remainder is computed as , with initial values and .| Step | (remainder) | Quotient | Expression for |
|---|---|---|---|
| 0 | 240 | - | |
| 1 | 46 | - | |
| 2 | 10 | 5 | |
| 3 | 6 | 4 | |
| 4 | 4 | 1 | |
| 5 | 2 | 1 | |
| 6 | 0 | 2 |
Mathematical Proof
The correctness of the extended Euclidean algorithm, which computes integers and such that for nonnegative integers , can be established by mathematical induction on , leveraging Bézout's identity that is the smallest positive linear combination of and .[15][16] Base case: If , then , and the algorithm returns , , satisfying . This holds since any integer divides zero, and the greatest divisor of and zero is itself.[15][17] Inductive step: Assume the algorithm is correct for all pairs with second argument less than ; that is, for the recursive call on where and , it returns integers and such that . Substituting yields , or . Thus, setting and satisfies , confirming the algorithm's output for the original pair.[15][16] The algorithm terminates because the underlying Euclidean algorithm does: each remainder strictly decreases () and remains nonnegative, eventually reaching zero after finitely many steps, at most iterations by properties of the Fibonacci sequence bounding the worst case.[18][15] The solution is not unique; all integer solutions to are given by and for integer , where is any particular solution and , since adding multiples of to and subtracting multiples of from preserves the equation due to .[19][20]Algorithmic Implementation
Recursive Formulation
The recursive formulation of the extended Euclidean algorithm computes the greatest common divisor of two integers and (assuming ), along with the Bézout coefficients and satisfying the equation . This version naturally extends the recursive structure of the standard Euclidean algorithm by tracking the coefficients through back-substitution in the recursion.[21] The function, typically denoted asextended_gcd(a, b), returns a tuple . In the base case, if , it returns , since and .[21]
For the recursive case, the algorithm calls itself on the pair to obtain where and . It then computes the coefficients for the original pair as and , before returning . This adjustment ensures the linear combination holds for and , mirroring the division step in the Euclidean algorithm. 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)
Iterative Pseudocode
The iterative formulation of the extended Euclidean algorithm computes the greatest common divisor along with Bézout coefficients and such that , 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.[24] 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 and :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
Generalizations
Polynomial Version
The extended Euclidean algorithm generalizes to univariate polynomials over a field , such as the rational numbers or real numbers, in the polynomial ring . Given two polynomials , not both zero, the algorithm computes a monic greatest common divisor and polynomials satisfying Bézout's identity . This adaptation relies on the fact that forms a Euclidean domain, where the degree function serves as the Euclidean norm, enabling division with remainder analogous to the integer case.[27][28] The algorithm proceeds recursively, mirroring the structure for integers but using polynomial division. Without loss of generality, assume . Divide by to obtain the quotient and remainder such that with . Recursively apply the algorithm to and , yielding polynomials and where . Back-substituting the expression for gives , so set and . The base cases occur when , in which (normalized to be monic) with and , or when is a nonzero constant, in which (the monic unit) and the coefficients are scaled accordingly by the leading coefficient of . Normalization ensures is monic by dividing by its leading coefficient throughout.[29][28] For illustration, consider and over the real numbers . Dividing by yields quotient and constant remainder . The recursion on and produces the monic gcd , and back-substitution provides explicit and satisfying the identity, though the full computation involves scaling by the leading coefficient of the remainder.[29] The computational complexity of this algorithm is arithmetic operations in the field , 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.[30]Extension to Multiple Arguments
The extended Euclidean algorithm can be generalized to compute the greatest common divisor of integers and express as an integer linear combination , where the coefficients are integers (not necessarily unique).[31] 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 as a linear combination and then incorporating the th via another application of the algorithm.[31] The method proceeds iteratively by pairwise application. Begin by applying the extended Euclidean algorithm to the first two integers and to obtain . Then, compute using the extended Euclidean algorithm again, and substitute the expression for to yield . Continue this process sequentially for each subsequent , updating the coefficients for prior terms by multiplication with the new intermediate coefficient and adding the new term's coefficient. This yields the full linear combination after applications.[32] For example, consider the integers 48, 18, and 30. First, apply the extended Euclidean algorithm to 48 and 18: Back-substituting gives . Now incorporate 30 by computing : Thus, . Substituting the prior expression yields . The coefficients are updated accordingly, confirming .[32] In this iterative approach, the coefficients can grow rapidly—potentially exponentially in —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 .[33] This growth makes the method less efficient for large , 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 binary GCD algorithm) 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.[34]Applications
Rational Number Simplification
To simplify a rational number expressed as a fraction where and are integers and , the extended Euclidean algorithm (EEA) is applied to compute the greatest common divisor .[35] The simplified form is then , with signs adjusted to ensure the denominator is positive (e.g., if , multiply numerator and denominator by -1).[25] This process reduces the fraction to its lowest terms by dividing both numerator and denominator by their common divisor .[36] The EEA is particularly efficient for this task because it computes the GCD in steps using repeated division, outperforming naive factorization methods for large integers.[35] Although the EEA also outputs Bézout coefficients expressing as an integer linear combination of and , these are incidental for simplification and not required.[25] For example, consider the fraction . Applying the EEA yields , so the simplified form is .[1] Edge cases must be handled carefully: if , the fraction is undefined; if , it simplifies to .[25] The steps of the EEA on and directly mirror the continued fraction expansion of , where the quotients from successive divisions form the continued fraction coefficients, terminating finitely for rationals.[37] 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.[35]Modular Multiplicative Inverses
In modular arithmetic, an integer has a multiplicative inverse modulo if and only if , meaning there exists an integer such that .[38] The extended Euclidean algorithm computes this inverse by finding integers and satisfying Bézout's identity , from which (adjusted to a positive representative between 0 and ) serves as the inverse.[38] To compute the inverse, apply the extended Euclidean algorithm to and :- Perform the standard Euclidean algorithm to find ; if it is not 1, no inverse exists.
- Use the extended version to back-substitute and express 1 as a linear combination .
- The coefficient is the inverse, taken modulo .
