Information about Extended Euclidean Algorithm
The extended Euclidean algorithm is an extension to the Euclidean algorithm for finding the greatest common divisor (GCD) of integers a and b: it also finds the integers x and y in Bézout's identity
(Typically either x or y is negative).
The extended Euclidean algorithm is particularly useful when a and b are coprime, since x is the modular multiplicative inverse of a modulo b.
It is assumed that the reader is already familiar with Euclid's algorithm.
To illustrate the extension of the Euclid's algorithm, consider the computation of gcd(120, 23), which is shown on the table on the left. Notice that the quotient in each division is recorded as well alongside the remainder.
In this case, the remainder in the last but one line (which is 1) indicates that the gcd is 1; that is, 120 and 23 are coprime (also called relatively prime). For the sake of simplicity, the example chosen is a coprime pair; but the more general case of gcd other than 1 also works similarly.
There are two methods to proceed, both using the division algorithm, which will be discussed separately.
The first line fits the pattern of the original equation, with 120 and 23 as the terms. However from the second line onwards, the terms are decreasing. To proceed, observe that by nature of the Euclidean algorithm:
Corresponding to this example, notice the divisor on the third line, 3, is the same as the remainder on the second line. Further, the dividend on the third line, 5, is the same as the divisor on the second line, and the remainder on the first line.
In the inductive method, the remainder at each step is written in terms of the integers a and b.
The last line reads 1 = −9×120 + 47×23, which is the required solution: x = −9 and y = 47.
This also means that −9 is the multiplicative inverse of 120 modulo 23, and that 47 is the multiplicative inverse of 23 modulo 120. Restating it using mathematical expression:
Consider the original equation:
Notice that the equation remains unchanged after decomposing the original dividend in terms of the divisor plus a remainder, and then regrouping terms. If we have a solution to the equation in the second line, then we can work backward to find x and y as required. Although we don't have the solution yet to the second line, notice how the magnitude of the terms decreased (120 and 23 to 23 and 5). Hence, if we keep applying this, eventually we'll reach the last line, which obviously has (1,0) as a trivial solution. Then we can work backward and gradually find out x and y.
For the purpose of explaining this method, the full working will not be shown. Instead some of the repeating steps will be described to demonstrate the principle behind this method.
Start by rewriting each line from the first table with division algorithm, focusing on the dividend this time (because we'll be substituting the dividend).
as a sequence of divisors
. In the running example we have the sequence 120, 23, 5, 3, 2, 1. Any element in this chain can be written as a linear combination of the original
and
, most notably, the last element,
, can be written in this way. The table method involves keeping a table of each divisor, written as a linear combination. The algorithm starts with the table as follows:
The elements in the
column of the table will be the divisors in the sequence. Each
can be represented as the linear combination
. The
and
values are obvious for the first two rows of the table, which represent
and
themselves. To compute
for any
, notice that
. Suppose
. Then it must be that
and
. This is easy to verify algebraically with a simple substitution.
Actually carrying out the table method though is simpler than the above equations would indicate. To find the third row of the table in the example, just notice that 120 divided by 23 goes 5 times plus a remainder. This gives us k, the multiplying factor for this row. Now, each value in the table is the value two rows above it, minus 5 times the value immediately above it. This correctly leads to
,
, and
. After repeating this method to find each line of the table, the final values for
and
will solve
:
This method is simple, requiring only the repeated application of one rule, and leaves the answer in the final row of the table with no backtracking. Note also that if you end up with a negative number as the answer for the factor of, in this case b, you will then need to add a in order to make it work as a modular inverse (instead of just taking the absolute value of b). I.e. if it returns a negative number, don't just flip the sign, but add in the other number to make it work. Otherwise it will give you the modular inverse yielding negative one.
Pseudocode for this method is shown below:
function extended_gcd(a, b) x := 0 lastx := 1 y := 1 lasty := 0 while b ≠ 0 temp := b quotient := a div b b := a mod b a := temp
temp := x x := lastx-quotient*x lastx := temp
temp := y y := lasty-quotient*y lasty := temp return {lastx, lasty, a}
Which can be directly translated to this pseudocode: function extended_gcd(a, b) if a mod b = 0 return {0, 1} else {x, y} := extended_gcd(b, a mod b) return {y, x-y*(a div b)}
pseudocode:
remainder[1] := f(x) remainder[2] := a(x) auxiliary[1] := 0 auxiliary[2] := 1 i := 2 do while remainder[i] > 1 i := i + 1 remainder[i] := remainder(remainder[i-2] / remainder[i-1]) quotient[i] := quotient(remainder[i-2] / remainder[i-1]) auxiliary[i] := -quotient[i] * auxiliary[i-1] + auxiliary[i-2] inverse := auxiliary[i]
auxiliary[i] := -quotient[i] * auxiliary[i-1] + auxiliary[i-2]
This is true since in the finite field GF(28), for instance, addition and subtraction are the same. In other words, 1 is its own additive inverse in GF(28).
Note: Addition in a binary finite field is XOR.
Thus, the inverse is x7 + x6 + x3 + x = {CA}, as can be confirmed by multiplying the two elements together.
..... Click the link for more information.
(Typically either x or y is negative).
The extended Euclidean algorithm is particularly useful when a and b are coprime, since x is the modular multiplicative inverse of a modulo b.
Informal formulation of the algorithm
| Dividend | Divisor | Quotient | Remainder |
|---|---|---|---|
| 120 | 23 | 5 | 5 |
| 23 | 5 | 4 | 3 |
| 5 | 3 | 1 | 2 |
| 3 | 2 | 1 | 1 |2||1||2||0 |
It is assumed that the reader is already familiar with Euclid's algorithm.
To illustrate the extension of the Euclid's algorithm, consider the computation of gcd(120, 23), which is shown on the table on the left. Notice that the quotient in each division is recorded as well alongside the remainder.
In this case, the remainder in the last but one line (which is 1) indicates that the gcd is 1; that is, 120 and 23 are coprime (also called relatively prime). For the sake of simplicity, the example chosen is a coprime pair; but the more general case of gcd other than 1 also works similarly.
There are two methods to proceed, both using the division algorithm, which will be discussed separately.
The iterative method
This method solves the required equation with the sum replaced by the remainders in each step of the algorithm, which is larger than their gcd, but are decreasing in magnitude, and so will eventually become the required equation. To start with, express each line from the last table with the division algorithm, focusing on the remainder:| Remainder | = | Dividend | - | Quotient | × | Divisor |
|---|---|---|---|---|---|---|
| 5 | = | 120 | - | 5 | × | 23 |
| 3 | = | 23 | - | 4 | × | 5 |
| 2 | = | 5 | - | 1 | × | 3 |
| 1 | = | 3 | - | 1 | × | 2 |0||=||2||-||2||×||1 |
The first line fits the pattern of the original equation, with 120 and 23 as the terms. However from the second line onwards, the terms are decreasing. To proceed, observe that by nature of the Euclidean algorithm:
- In each line, the divisor is the remainder of the previous line
- The dividend is the previous line's divisor, which is then remainder of its previous line(that is, remainder from two lines up)
Corresponding to this example, notice the divisor on the third line, 3, is the same as the remainder on the second line. Further, the dividend on the third line, 5, is the same as the divisor on the second line, and the remainder on the first line.
In the inductive method, the remainder at each step is written in terms of the integers a and b.
| 5 | = | 120 | - | 5 | × | 23 | = | = | 1 | × | 120 | - | 5 | × | 23 | |||||
| 3 | = | 23 | - | 4 | × | 5 | = | 1×23 | - | 4 | × | (1×120 - 5×23) | = | -4 | × | 120 | + | 21 | × | 23 |
| 2 | = | 5 | - | 1 | × | 3 | = | (1×120 - 5×23) | - | 1 | × | (-4×120 + 21×23) | = | 5 | × | 120 | - | 26 | × | 23 |1|| = ||3|| - ||1||×||2 | = ||(-4×120 + 21×23)|| - ||1||×||(5×120 - 26×23) | = ||-9||×||120|| + ||47||×||23 |
- The first line already in the required form.
- In the second line, substitute 5 with the first line.
- In the third line, substitute 5 and 3 with the first and second line.
- In the fourth line, substitute 3 and 2 with the second and third line.
The last line reads 1 = −9×120 + 47×23, which is the required solution: x = −9 and y = 47.
This also means that −9 is the multiplicative inverse of 120 modulo 23, and that 47 is the multiplicative inverse of 23 modulo 120. Restating it using mathematical expression:
- −9 × 120 ≡ 1 (mod 23) and also 47 × 23 ≡ 1 (mod 120).
The recursive method
This method attempts to solve the original equation directly, by reducing the dividend and divisor gradually, from the first line to the last line, which can then be substituted with trivial value and work backward to obtain the solution.Consider the original equation:
| 120 | x | + | 23 | y | = | 1 |
| (5×23+5) | x | + | 23 | y | = | 1 |
| 23 | (5x+y) | + | 5 | x | = | 1 |
| ... | ||||||
| 1 | a | + | 0 | b | = | 1 |
Notice that the equation remains unchanged after decomposing the original dividend in terms of the divisor plus a remainder, and then regrouping terms. If we have a solution to the equation in the second line, then we can work backward to find x and y as required. Although we don't have the solution yet to the second line, notice how the magnitude of the terms decreased (120 and 23 to 23 and 5). Hence, if we keep applying this, eventually we'll reach the last line, which obviously has (1,0) as a trivial solution. Then we can work backward and gradually find out x and y.
| Dividend | = | Quotient | x | Divisor | + | Remainder |
|---|---|---|---|---|---|---|
| 120 | = | 5 | x | 23 | + | 5 |
| 23 | = | 4 | x | 5 | + | 3 |align="center" colspan=7|... |
For the purpose of explaining this method, the full working will not be shown. Instead some of the repeating steps will be described to demonstrate the principle behind this method.
Start by rewriting each line from the first table with division algorithm, focusing on the dividend this time (because we'll be substituting the dividend).
| 120 | x0 | + | 23 | y0 | = | 1 |
| (5×23+5) | x0 | + | 23 | y0 | = | 1 |
| 23 | (5x0+y0) | + | 5 | x0 | = | 1 |
| 23 | x1 | + | 5 | y1 | = | 1 |
| (4×5+3) | x1 | + | 5 | y1 | = | 1 |
| 5 | (4x1+y1) | + | 3 | x1 | = | 1 |5||x2||+||3||y2||=||1 |
- Assume that we were given x2=2 and y2=-3 already, which is indeed a valid solution.
- x1=y2=-3
- Solve 4x1+y1=x2 by substituting x1=-3, which gives y1=2-4(-3)=14
- x0=y1=14
- Solve 5x0+y0=x1 by substituting x0=14, so y0=-3-5(14)=-73
The table method
The table method is probably the simplest method to carry out with a pencil and paper. It is similar to the recursive method, although it does not directly require algebra to use and only requires working in one direction. The main idea is to think of the equation chain
as a sequence of divisors
. In the running example we have the sequence 120, 23, 5, 3, 2, 1. Any element in this chain can be written as a linear combination of the original
and
, most notably, the last element,
, can be written in this way. The table method involves keeping a table of each divisor, written as a linear combination. The algorithm starts with the table as follows:
| a | b | d |
| 1 | 0 | 120 |0||1||23 |
The elements in the
column of the table will be the divisors in the sequence. Each
can be represented as the linear combination
. The
and
values are obvious for the first two rows of the table, which represent
and
themselves. To compute
for any
, notice that
. Suppose
. Then it must be that
and
. This is easy to verify algebraically with a simple substitution.
Actually carrying out the table method though is simpler than the above equations would indicate. To find the third row of the table in the example, just notice that 120 divided by 23 goes 5 times plus a remainder. This gives us k, the multiplying factor for this row. Now, each value in the table is the value two rows above it, minus 5 times the value immediately above it. This correctly leads to
,
, and
. After repeating this method to find each line of the table, the final values for
and
will solve
:
| a | b | d |
| 1 | 0 | 120 |
| 0 | 1 | 23 |
| 1 | -5 | 5 |
| -4 | 21 | 3 |
| 5 | -26 | 2 | -9 ||47||1 |
This method is simple, requiring only the repeated application of one rule, and leaves the answer in the final row of the table with no backtracking. Note also that if you end up with a negative number as the answer for the factor of, in this case b, you will then need to add a in order to make it work as a modular inverse (instead of just taking the absolute value of b). I.e. if it returns a negative number, don't just flip the sign, but add in the other number to make it work. Otherwise it will give you the modular inverse yielding negative one.
Formal description of the algorithm
Iterative method
By routine algebra of expanding and grouping like terms (refer to last section), the following algorithm for iterative method is obtained:- Apply Euclidean algorithm, and let qn(n starts from 1) be a finite list of quotients in the division.
- Initialize x0, x1 as 1, 0, and y0, y1 as 0,1 respectively.
- Then for each i so long as qi is defined,
- Compute xi+1= xi-1- qixi
- Compute yi+1= yi-1- qiyi
- Repeat the above after incrementing i by 1.
- The answers are the second-to-last of xn and yn.
Pseudocode for this method is shown below:
function extended_gcd(a, b) x := 0 lastx := 1 y := 1 lasty := 0 while b ≠ 0 temp := b quotient := a div b b := a mod b a := temp
temp := x x := lastx-quotient*x lastx := temp
temp := y y := lasty-quotient*y lasty := temp return {lastx, lasty, a}
Recursive method
Solving the general case of the equation in the last corresponding section, the following algorithm results:- If a is divisible by b, the algorithm ends and return the trivial solution x=0, y=1.
- Otherwise, repeat the algorithm with b and a modulus b, storing the solution as x' and y'.
- Then, the solution to the current equation is x=y', and y = x' minus y' times quotient of a divided by b
Which can be directly translated to this pseudocode: function extended_gcd(a, b) if a mod b = 0 return {0, 1} else {x, y} := extended_gcd(b, a mod b) return {y, x-y*(a div b)}
Proof of correctness
Let d be the gcd of a and b. We wish to prove that a*x + b*y = d.- If b evenly divides a (i.e. a mod b = 0),
- then d is b and a*0 + b*1 = d.
- So x and y are 0 and 1.
- Otherwise given the recursive call we know that b*x + (a mod b) * y = d,
- then b*x - b*(a div b)*y + (a mod b) * y + b*(a div b)*y= d,
- and b*(x - (a div b)*y) + a*y=d.
- So the new x and y are y and x - (a div b)*y.
Computing a multiplicative inverse in a finite field
The extended Euclidean algorithm can also be used to calculate the multiplicative inverse in a finite field.Pseudocode
Given the irreducible polynomial f(x) used to define the finite field, and the element a(x) whose inverse is desired, then a form of the algorithm suitable for determining the inverse is given by the following. NOTE: remainder() and quotient() are functions different from the arrays remainder[ ] and quotient[ ]. remainder() refers to the remainder when two numbers are divided, and quotient() refers to the integer quotient when two numbers are divided. For example, remainder(5/3) = 2 and quotient(5/3) = 1. Equivalent operators in the C language are % and / respectively.pseudocode:
remainder[1] := f(x) remainder[2] := a(x) auxiliary[1] := 0 auxiliary[2] := 1 i := 2 do while remainder[i] > 1 i := i + 1 remainder[i] := remainder(remainder[i-2] / remainder[i-1]) quotient[i] := quotient(remainder[i-2] / remainder[i-1]) auxiliary[i] := -quotient[i] * auxiliary[i-1] + auxiliary[i-2] inverse := auxiliary[i]
Note
The minus sign is not necessary for some finite fields in the step.auxiliary[i] := -quotient[i] * auxiliary[i-1] + auxiliary[i-2]
This is true since in the finite field GF(28), for instance, addition and subtraction are the same. In other words, 1 is its own additive inverse in GF(28).
Example
For example, if the polynomial used to define the finite field GF(28) is f(x) = x8 + x4 + x3 + x + 1, and x6 + x4 + x + 1 = {53} in big-endian hexadecimal notation, is the element whose inverse is desired, then performing the algorithm results in the following:| i | remainder[i] | quotient[i] | auxiliary[i] |
|---|---|---|---|
| 1 | x8 + x4 + x3 + x + 1 | 0 | |
| 2 | x6 + x4 + x + 1 | 1 | |
| 3 | x2 | x2 + 1 | x2 + 1 |
| 4 | x + 1 | x4 + x2 | x6 + |
| 5 | 1 | x + 1 | x7 + x6 + x3 + |
Thus, the inverse is x7 + x6 + x3 + x = {CA}, as can be confirmed by multiplying the two elements together.
References
- 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.
See also
External links
- A php implementation of the Extended Euclidean Algorithm showing line-by-line working and outputs
- A javascript implementation of the Extended Euclidean Algorithm shown above
- How to use the algorithm by hand
- Extended Euclidean Algorithm Applet
- Source for the form of the algorithm used to determine the multiplicative inverse in GF(2^8)
- A simple explain of Euclidean Algorithm
Euclidean algorithm (also called Euclid's algorithm) is an algorithm to determine the greatest common divisor (GCD) of two elements of any Euclidean domain (for example, the integers).
..... Click the link for more information.
..... Click the link for more information.
In mathematics, the greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers without remainder.
..... Click the link for more information.
..... Click the link for more information.
In number theory, Bézout's identity or Bézout's lemma is a linear diophantine equation. It states that if a and b are nonzero integers with greatest common divisor d, then there exist integers x and y (called
..... Click the link for more information.
..... Click the link for more information.
In mathematics, the integers a and b are said to be coprime or relatively prime if they have no common factor other than 1 and −1, or equivalently, if their greatest common divisor is 1.
..... Click the link for more information.
..... Click the link for more information.
The modular multiplicative inverse of an integer n modulo p is an integer m such that
..... Click the link for more information.
- n-1 ≡ m (mod p)
..... Click the link for more information.
Modular arithmetic (sometimes called modulo arithmetic, or clock arithmetic) is a system of arithmetic for integers, where numbers "wrap around" after they reach a certain value — the modulus.
..... Click the link for more information.
..... Click the link for more information.
Euclidean algorithm (also called Euclid's algorithm) is an algorithm to determine the greatest common divisor (GCD) of two elements of any Euclidean domain (for example, the integers).
..... Click the link for more information.
..... Click the link for more information.
In mathematics, the integers a and b are said to be coprime or relatively prime if they have no common factor other than 1 and −1, or equivalently, if their greatest common divisor is 1.
..... Click the link for more information.
..... Click the link for more information.
division algorithm is a theorem in mathematics which precisely expresses the outcome of the usual process of division of integers. The name is something of a misnomer, as it is a theorem, not an algorithm, i.e.
..... Click the link for more information.
..... Click the link for more information.
division algorithm is a theorem in mathematics which precisely expresses the outcome of the usual process of division of integers. The name is something of a misnomer, as it is a theorem, not an algorithm, i.e.
..... Click the link for more information.
..... Click the link for more information.
Mathematical induction is a method of mathematical proof typically used to establish that a given statement is true of all natural numbers. It is done by
..... Click the link for more information.
- proving that the first statement in the infinite sequence of statements is true, and then
..... Click the link for more information.
multiplicative inverse for a number x, denoted by 1⁄x or x −1, is a number which when multiplied by x yields the multiplicative identity, 1.
..... Click the link for more information.
..... Click the link for more information.
Euclidean algorithm (also called Euclid's algorithm) is an algorithm to determine the greatest common divisor (GCD) of two elements of any Euclidean domain (for example, the integers).
..... Click the link for more information.
..... Click the link for more information.
multiplicative inverse for a number x, denoted by 1⁄x or x −1, is a number which when multiplied by x yields the multiplicative identity, 1.
..... Click the link for more information.
..... Click the link for more information.
In abstract algebra, a finite field or Galois field (so named in honor of Évariste Galois) is a field that contains only finitely many elements. Finite fields are important in number theory, algebraic geometry, Galois theory, cryptography, and coding theory.
..... Click the link for more information.
..... Click the link for more information.
Pseudocode (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax.
..... Click the link for more information.
..... Click the link for more information.
In abstract algebra, a finite field or Galois field (so named in honor of Évariste Galois) is a field that contains only finitely many elements. Finite fields are important in number theory, algebraic geometry, Galois theory, cryptography, and coding theory.
..... Click the link for more information.
..... Click the link for more information.
In computing, endianness is the byte (and sometimes bit) ordering in memory used to represent some kind of data. Typical cases are the order in which integer values are stored as bytes in computer memory (relative to a given memory addressing scheme) and the transmission order over
..... Click the link for more information.
..... Click the link for more information.
hexadecimal, base-16, or simply hex, is a numeral system with a radix, or base, of 16, usually written using the symbols 0–9 and A–F, or a–f.
..... Click the link for more information.
..... Click the link for more information.
exclusive disjunction, also called exclusive or, (symbolized XOR or EOR), is a type of logical disjunction on two operands that results in a value of "true" if and only if exactly one of the operands has a value of "true.
..... Click the link for more information.
..... Click the link for more information.
Arithmetic in a finite field is different from standard integer arithmetic. There are a limited number of elements in the finite field; all operations performed in the finite field result in an element within that field.
..... Click the link for more information.
..... Click the link for more information.
Thomas H. Cormen is the co-author of Introduction to Algorithms, along with Charles Leiserson, Ron Rivest, and Cliff Stein. He is a Full Professor of computer science at Dartmouth College.
..... Click the link for more information.
..... Click the link for more information.
Charles E. Leiserson is a computer scientist, specializing in the theory of parallel computing and distributed computing, and particularly practical applications thereof; as part of this effort, he developed the Cilk multithreaded language.
..... Click the link for more information.
..... Click the link for more information.
..... Click the link for more information.
Clifford Stein is a computer scientist, currently working as a professor at Columbia University in New York, NY. He earned his BSE from Princeton University in 1987, a MS from Massachusetts Institute of Technology in 1989, and a PhD from Massachusetts Institute of Technology in
..... Click the link for more information.
..... Click the link for more information.
Introduction to Algorithms is a book by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. It is used as the textbook for algorithms courses at many universities.
..... Click the link for more information.
..... Click the link for more information.
In number theory, Bézout's identity or Bézout's lemma is a linear diophantine equation. It states that if a and b are nonzero integers with greatest common divisor d, then there exist integers x and y (called
..... Click the link for more information.
..... Click the link for more information.
This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus
