Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Extended Euclidean algorithm
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Method for computing the relation of two integers with their greatest common divisor}} 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 : <math>ax + by = \gcd(a, b).</math> This is a [[certifying algorithm]], because the gcd is the only number that can simultaneously satisfy this equation and divide the inputs.<ref>{{cite web |last1=McConnell |first1=Ross |last2=Mehlhorn |first2=Kurt |last3=Näher |first3=Stefan |last4=Schweitzer |first4=Pascal |title=Certifying Algorithms |url=https://people.mpi-inf.mpg.de/~mehlhorn/ftp/CertifyingAlgorithms.pdf |access-date=29 September 2024}}</ref> It allows one to compute also, with almost no extra cost, the quotients of ''a'' and ''b'' by their greatest common divisor. {{em|Extended Euclidean algorithm}} also refers to a [[Polynomial greatest common divisor#Bézout's identity and extended GCD algorithm|very similar algorithm]] for computing the [[polynomial greatest common divisor]] and the coefficients of Bézout's identity of two [[univariate polynomial]]s. The extended Euclidean algorithm is particularly useful when ''a'' and ''b'' are [[coprime]]. With that provision, ''x'' is the [[modular multiplicative inverse]] of ''a'' [[modular arithmetic|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 extension]]s and, in particular in [[finite field]]s 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 (algorithm)|RSA]] public-key encryption method. == Description== The standard Euclidean algorithm proceeds by a succession of [[Euclidean division]]s 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 <math>q_1,\ldots, q_k</math> of quotients and a sequence <math>r_0,\ldots, r_{k+1}</math> of remainders such that :<math> \begin{align} r_0 & =a \\ r_1 & =b \\ & \,\,\,\vdots \\ r_{i+1} & =r_{i-1}-q_i r_i \quad \text {and} \quad 0\le r_{i+1} < |r_i| \quad\text{(this defines }q_i)\\ & \,\,\, \vdots \end{align} </math> It is the main property of [[Euclidean division]] that the inequalities on the right define uniquely <math>q_i</math> and <math>r_{i+1}</math> from <math>r_{i-1}</math> and <math>r_{i}.</math> The computation stops when one reaches a remainder <math>r_{k+1}</math> which is zero; the greatest common divisor is then the last non zero remainder <math>r_{k}.</math> The extended Euclidean algorithm proceeds similarly, but adds two other sequences, as follows :<math> \begin{align} r_0 & =a & r_1 & =b \\ s_0 & =1 & s_1 & =0 \\ t_0 & =0 & t_1 & =1 \\ & \,\,\,\vdots & & \,\,\,\vdots \\ r_{i+1} & =r_{i-1}-q_i r_i & \text {and } 0 & \le r_{i+1} < |r_i| & \text{(this defines } q_i \text{)}\\ s_{i+1} & =s_{i-1}-q_i s_i \\ t_{i+1} & =t_{i-1}-q_i t_i \\ & \,\,\, \vdots \end{align} </math> The computation also stops when <math>r_{k+1}=0</math> and gives *<math>r_k</math> is the greatest common divisor of the input <math>a=r_0</math> and <math>b=r_1.</math> * The Bézout coefficients are <math>s_k</math> and <math>t_k,</math> that is <math>\gcd(a,b)=r_k=as_k+bt_k</math> * The quotients of ''a'' and ''b'' by their greatest common divisor are given by <math>s_{k+1}=\pm\frac{b}{\gcd(a,b)}</math> and <math>t_{k+1}=\pm\frac{a}{\gcd(a,b)}</math> Moreover, if ''a'' and ''b'' are both positive and <math>\gcd(a,b)\neq\min(a,b)</math>, then :<math>|s_i| \leq \left\lfloor\frac{b}{2\gcd(a,b)}\right\rfloor\quad \text{and} \quad |t_i| \leq \left\lfloor\frac{a}{2\gcd(a,b)}\right\rfloor</math> for <math>0\leq i \leq k,</math> where <math>\lfloor x\rfloor</math> denotes the [[integral part]] of {{mvar|x}}, that is the greatest integer not greater than {{mvar|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 === The following table shows how the extended Euclidean algorithm proceeds with input {{nowrap|{{green|240}}}} and {{nowrap|{{green|46}}}}. The greatest common divisor is the last non zero entry, {{nowrap|{{red|2}}}} in the column "remainder". The computation stops at row 6, because the remainder in it is {{nowrap|{{red|0}}}}. Bézout coefficients appear in the last two columns of the second-to-last row. In fact, it is easy to verify that {{nowrap|1={{magenta|−9}} × {{green|240}} + {{magenta|47}} × {{green|46}} = {{red|2}}}}. Finally the last two entries {{nowrap|{{cyan|23}}}} and {{nowrap|{{cyan|−120}}}} of the last row are, up to the sign, the quotients of the input {{nowrap|{{green|46}}}} and {{nowrap|{{green|240}}}} by the greatest common divisor {{nowrap|{{red|2}}}}. {| class="wikitable" style="text-align:right;" ! index ''i''!! {{blue|quotient ''q''<sub>''i''−1</sub> }}!! {{olive|Remainder ''r''<sub>''i''</sub>}}!! {{brown|''s''<sub>''i''</sub> }}!! ''t''<sub>''i''</sub> |- | 0 || ||{{green|240}}||{{brown|1}} || 0 |- | 1 || ||{{green|46}} || {{brown|0}} || 1 |- | 2 ||{{green|240}} ÷ {{green|46}} = {{blue|5}} ||{{green|240}} − {{blue|5}} × {{green|46}} = {{olive|10}} ||{{brown|1}} − {{blue|5}} × {{brown|0}} = {{brown|1}} || 0 − {{blue|5}} × 1 = −5 |- | 3 ||{{green|46}} ÷ {{olive|10}} = {{blue|4}} ||{{green|46}} − {{blue|4}} × {{olive|10}} = {{olive|6}} ||{{brown|0}} − {{blue|4}} × {{brown|1}} = {{brown|−4}} || 1 − {{blue|4}} × −5 = 21 |- | 4 ||{{olive|10}} ÷ {{olive|6}} = {{blue|1}} ||{{olive|10}} − {{blue|1}} × {{olive|6}} = {{olive|4}} ||{{brown|1}} − {{blue|1}} × {{brown|−4}} = {{brown|5}} || −5 − {{blue|1}} × 21 = −26 |- | 5 ||{{olive|6}} ÷ {{olive|4}} = {{blue|1}} ||{{olive|6}} − {{blue|1}} × {{olive|4}} = {{red|2}} ||{{brown|−4}} − {{blue|1}} × {{brown|5}} = {{magenta|−9}} || 21 − {{blue|1}} × −26 = {{magenta|47}} |- | 6 ||{{olive|4}} ÷ {{olive|2}} = {{blue|2}} ||{{olive|4}} − {{blue|2}} × {{olive|2}} = {{red|0}} ||{{brown|5}} − {{blue|2}} × {{brown|−9}} = {{cyan|23}} || −26 − {{blue|2}} × 47 = {{cyan|−120}} |} === Proof === As <math> 0\le r_{i+1}<|r_i|, </math> the sequence of the <math> r_i </math> is a decreasing sequence of nonnegative integers (from ''i'' = 2 on). Thus it must stop with some <math> r_{k+1}=0.</math> This proves that the algorithm stops eventually. As <math> r_{i+1}= r_{i-1} - r_i q_i,</math> the greatest common divisor is the same for <math>(r_{i-1}, r_i)</math> and <math>(r_{i}, r_{i+1}).</math> This shows that the greatest common divisor of the input <math>a=r_0, b=r_1 </math> is the same as that of <math> r_k, r_{k+1}=0.</math> This proves that <math> r_k </math> 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 <math> a=r_0</math> and <math> b=r_1,</math> we have <math>as_i+bt_i=r_i</math> for ''i'' = 0 and 1. The relation follows by induction for all <math>i>1</math>: <math display="block">r_{i+1} = r_{i-1} - r_i q_i = (as_{i-1}+bt_{i-1}) - (as_i+bt_i)q_i = (as_{i-1}-as_iq_i) + (bt_{i-1}-bt_iq_i) = as_{i+1}+bt_{i+1}.</math> Thus <math>s_k</math> and <math>t_k</math> are Bézout coefficients. Consider the matrix <math display="block">A_i=\begin{pmatrix} s_{i-1} & s_i\\ t_{i-1} & t_i \end{pmatrix}.</math> The recurrence relation may be rewritten in matrix form <math display="block">A_{i+1} = A_i \cdot \begin{pmatrix} 0 & 1\\ 1 & -q_i \end{pmatrix}.</math> The matrix <math>A_1</math> 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 <math>A_i</math> is <math>(-1)^{i-1}.</math> In particular, for <math>i=k+1,</math> we have <math>s_k t_{k+1} - t_k s_{k+1} = (-1)^k.</math> Viewing this as a Bézout's identity, this shows that <math>s_{k+1}</math> and <math>t_{k+1}</math> are [[coprime]]. The relation <math>as_{k+1}+bt_{k+1}=0</math> that has been proved above and [[Euclid's lemma]] show that <math>s_{k+1}</math> divides {{mvar|b}}, that is that <math>b=ds_{k+1}</math> for some integer {{mvar|d}}. Dividing by <math>s_{k+1}</math> the relation <math>as_{k+1}+bt_{k+1}=0</math> gives <math>a=-dt_{k+1}.</math> So, <math>s_{k+1}</math> and <math>-t_{k+1}</math> are coprime integers that are the quotients of {{mvar|a}} and {{mvar|b}} by a common factor, which is thus their greatest common divisor or its [[additive inverse|opposite]]. To prove the last assertion, assume that ''a'' and ''b'' are both positive and <math>\gcd(a,b)\neq\min(a,b)</math>. Then, <math>a \neq b </math>, and if <math>a < b</math>, 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 <math>a > b</math> [[without loss of generality]]. It can be seen that <math>s_2</math> is 1 and <math>s_3</math> (which exists by <math>\gcd(a,b)\neq\min(a,b)</math>) is a negative integer. Thereafter, the <math>s_i</math> alternate in sign and strictly increase in magnitude, which follows inductively from the definitions and the fact that <math>q_i\geq 1</math> for <math>1 \leq i \leq k</math>, the case <math>i = 1</math> holds because <math>a > b</math>. The same is true for the <math>t_i</math> after the first few terms, for the same reason. Furthermore, it is easy to see that <math>q_k \geq 2</math> (when ''a'' and ''b'' are both positive and <math>\gcd(a,b)\neq\min(a,b)</math>). Thus, noticing that <math>|s_{k+1}| = |s_{k-1}| + q_k |s_k|</math>, we obtain <math display="block">|s_{k+1}|=\left |\frac{b}{\gcd(a,b)} \right | \geq 2|s_k| \qquad \text{and} \qquad |t_{k+1}|= \left |\frac{a}{\gcd(a,b)} \right | \geq 2|t_k|.</math> This, accompanied by the fact that <math>s_k,t_k</math> are larger than or equal to in absolute value than any previous <math>s_i</math> or <math>t_i</math> respectively completed the proof. == Polynomial extended Euclidean algorithm == {{see also|Polynomial greatest common divisor#Bézout's identity and extended GCD algorithm}} For [[univariate polynomial]]s with coefficients in a [[field (mathematics)|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 <math>0\le r_{i+1}<|r_i|</math> has to be replaced by an inequality on the degrees <math>\deg r_{i+1}<\deg r_i.</math> 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'' :<math>as+bt=\gcd(a,b)</math> ''and'' :<math>\deg s < \deg b - \deg (\gcd(a,b)), \quad \deg t < \deg a - \deg (\gcd(a,b)).</math> A third difference is that, in the polynomial case, the greatest common divisor is defined only up to the multiplication by a non zero 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 <math>r_{k}.</math> 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 non-zero 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 (algebra)|content]] of <math>r_{k},</math> to get a [[primitive polynomial (ring theory)|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 [[Polynomial greatest common divisor#Subresultant pseudo-remainder sequence|subresultant pseudo-remainder sequence]]s 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 <math>r_i</math> is a [[subresultant|subresultant polynomial]]. In particular, if the input polynomials are coprime, then the Bézout's identity becomes :<math>as+bt=\operatorname{Res}(a,b),</math> where <math>\operatorname{Res}(a,b)</math> 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== {{hatnote|In this section and the following ones, '''''div''''' is an auxiliary function that computes the quotient of the [[Euclidean division]] of its left argument by its right argument.}} 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 assignment]]s. 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, <math>ax + by = \gcd(a, b)</math>, one can solve for <math>y</math> given <math>a, b, x, \gcd(a, b)</math>. Thus, an optimization to the above algorithm is to compute only the <math>s_k</math> sequence (which yields the Bézout coefficient <math>x</math>), and then compute <math>y</math> 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== A fraction {{math|{{sfrac|''a''|''b''}}}} is in canonical simplified form if {{math|''a''}} and {{math|''b''}} are [[coprime]] and {{math|''b''}} is positive. This canonical simplified form can be obtained by replacing the three output lines of the preceding pseudo code by '''if''' {{math|1=''s'' = 0}} '''then output''' "Division by zero" '''if''' {{math|1=''s'' < 0}} '''then''' {{math|1=''s'' := −''s''}}; {{math|1=''t'' := −''t''}} (''for avoiding negative denominators'') '''if''' {{math|1=''s'' = 1}} '''then output''' {{math|−''t''}} (''for avoiding denominators equal to'' 1) '''output''' {{math|{{sfrac|−''t''|''s''}}}} The proof of this algorithm relies on the fact that {{math|''s''}} and {{math|''t''}} are two coprime integers such that {{math|1=''as'' + ''bt'' = 0}}, and thus <math>\frac{a}{b} = -\frac{t}{s}</math>. To get the canonical simplified form, it suffices to move the minus sign for having a positive denominator. If {{math|''b''}} divides {{math|''a''}} evenly, the algorithm executes only one iteration, and we have {{math|1=''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== The extended Euclidean algorithm is the essential tool for computing [[multiplicative inverse]]s in modular structures, typically the [[modular arithmetic|modular integer]]s and the [[algebraic field extension]]s. A notable instance of the latter case are the finite fields of non-prime order. ===Modular integers=== {{main|Modular arithmetic}} If {{math|''n''}} is a positive integer, the [[Ring_(mathematics)|ring]] {{math|[[Z/nZ|'''Z'''/''n'''''Z''']]}} may be identified with the set {{math|{0, 1, ..., ''n''-1}{{void}}}} of the remainders of [[Euclidean division]] by {{math|''n''}}, the addition and the multiplication consisting in taking the remainder by {{math|''n''}} of the result of the addition and the multiplication of integers. An element {{math|''a''}} of {{math|'''Z'''/''n'''''Z'''}} has a multiplicative inverse (that is, it is a [[unit (ring theory)|unit]]) if it is [[coprime]] to {{math|''n''}}. In particular, if {{math|''n''}} is [[prime number|prime]], {{math|''a''}} has a multiplicative inverse if it is not zero (modulo {{math|''n''}}). Thus {{math|'''Z'''/''n'''''Z'''}} is a field if and only if {{math|''n''}} is prime. Bézout's identity asserts that {{math|''a''}} and {{math|''n''}} are coprime if and only if there exist integers {{math|''s''}} and {{math|''t''}} such that :<math>ns+at=1</math> Reducing this identity modulo {{math|''n''}} gives :<math>at \equiv 1 \mod n.</math> Thus {{math|''t''}}, or, more exactly, the remainder of the division of {{math|''t''}} by {{math|''n''}}, is the multiplicative inverse of {{math|''a''}} modulo {{math|''n''}}. To adapt the extended Euclidean algorithm to this problem, one should remark that the Bézout coefficient of {{math|''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 {{math|''t''}} provided by the algorithm satisfies {{math|{{!}}''t''{{!}} < ''n''}}. That is, if {{math|''t'' < 0}}, one must add {{math|''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 === The extended Euclidean algorithm is also the main tool for computing [[multiplicative inverse]]s in [[simple extension|simple algebraic field extensions]]. An important case, widely used in [[cryptography]] and [[coding theory]], is that of [[finite field]]s of non-prime order. In fact, if {{math|''p''}} is a prime number, and {{math|1=''q'' = ''p''<sup>''d''</sup>}}, the field of order {{math|''q''}} is a simple algebraic extension of the [[prime field]] of {{math|''p''}} elements, generated by a root of an [[irreducible polynomial]] of degree {{math|''d''}}. A simple algebraic extension {{math|''L''}} of a field {{math|''K''}}, generated by the root of an irreducible polynomial {{math|''p''}} of degree {{math|''d''}} may be identified to the [[quotient ring]] <math>K[X]/\langle p\rangle,</math>, and its elements are in [[bijective|bijective correspondence]] with the polynomials of degree less than {{math|''d''}}. The addition in {{math|''L''}} is the addition of polynomials. The multiplication in {{math|''L''}} is the remainder of the [[Euclidean division of polynomials|Euclidean division]] by {{math|''p''}} of the product of polynomials. Thus, to complete the arithmetic in {{math|''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 {{math|''d''}}. Secondly, the greatest common divisor which is provided, when the input polynomials are coprime, may be any non zero elements of {{math|''K''}}; this Bézout coefficient (a polynomial generally of positive degree) has thus to be multiplied by the inverse of this element of {{math|''K''}}. In the pseudocode which follows, {{math|''p''}} is a polynomial of degree greater than one, and {{math|''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==== For example, if the polynomial used to define the finite field GF(2<sup>8</sup>) is {{math|1=''p'' = ''x''<sup>8</sup> + ''x''<sup>4</sup> + ''x''<sup>3</sup> + ''x'' + 1}}, and {{math|1=''a'' = ''x''<sup>6</sup> + ''x''<sup>4</sup> + ''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 2<sup>''n''</sup>, 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. {| class="wikitable" |- ! step ! quotient ! r, newr ! s, news ! t, newt |- ! | | {{math|1=''p'' = ''x''<sup>8</sup> + ''x''<sup>4</sup> + ''x''<sup>3</sup> + ''x'' + 1 }} |1 | 0 |- ! | | {{math|1=''a'' = ''x''<sup>6</sup> + ''x''<sup>4</sup> + ''x'' + 1}} |0 | 1 |- ! 1 | {{math|1=''x''<sup>2</sup> + 1}} | {{math|1=''x''<sup>2</sup> = ''p'' − ''a'' (''x''<sup>2</sup> + 1)}} |1 | {{math|1=''x''<sup>2</sup> + 1 = 0 − 1 · (''x''<sup>2</sup> + 1)}} |- ! 2 | {{math|1=''x''<sup>4</sup> + ''x''<sup>2</sup>}} | {{math|1=''x'' + 1 = ''a'' − ''x''<sup>2</sup> (''x''<sup>4</sup> + ''x''<sup>2</sup>)}} | {{math|1=''x''<sup>4</sup>+''x''<sup>2</sup> = 0 − 1(''x''<sup>4</sup>+''x''<sup>2</sup>)}} | {{math|1=''x''<sup>6</sup> + ''x''<sup>2</sup> + 1 = 1 − (''x''<sup>4</sup> + ''x''<sup>2</sup>) (''x''<sup>2</sup> + 1)}} |- ! 3 | {{math|1=''x'' + 1}} | {{math|1= 1 = ''x''<sup>2</sup> − (''x'' + 1) (''x'' + 1)}} | {{math|1=''x''<sup>5</sup>+''x''<sup>4</sup>+''x''<sup>3</sup>+''x''<sup>2</sup>+1 = 1 − (''x'' +1)(''x''<sup>4</sup> + ''x''<sup>2</sup>)}} | {{math|1=''x''<sup>7</sup> + ''x''<sup>6</sup> + ''x''<sup>3</sup> + ''x'' = (''x''<sup>2</sup> + 1) − (''x'' + 1) (''x''<sup>6</sup> + ''x''<sup>2</sup> + 1)}} |- ! 4 | {{math|1=''x'' + 1}} | {{math|1=0 = (''x'' + 1) − 1 × (''x'' + 1)}} | {{math|1=''x''<sup>6</sup> + ''x''<sup>4</sup> + ''x'' + 1 = (''x''<sup>4</sup>+''x''<sup>2</sup>) − (''x''+1)(''x''<sup>5</sup>+''x''<sup>4</sup>+''x''<sup>3</sup>+''x''<sup>2</sup>+1)}} | |} Thus, the inverse is {{math|''x''<sup>7</sup> + ''x''<sup>6</sup> + ''x''<sup>3</sup> + ''x''}}, as can be confirmed by [[finite field arithmetic|multiplying the two elements together]], and taking the remainder by {{mvar|p}} of the result. ==The case of more than two numbers == One can handle the case of more than two numbers iteratively. First we show that <math>\gcd(a,b,c) = \gcd(\gcd(a,b),c)</math>. To prove this let <math>d=\gcd(a,b,c)</math>. By definition of gcd <math>d</math> is a divisor of <math>a</math> and <math>b</math>. Thus <math>\gcd(a,b)=k d</math> for some <math>k</math>. Similarly <math>d</math> is a divisor of <math>c</math> so <math>c=jd</math> for some <math>j</math>. Let <math>u=\gcd(k,j)</math>. By our construction of <math>u</math>, <math>ud | a,b,c</math> but since <math>d</math> is the greatest divisor <math>u</math> is a [[Unit (ring theory)|unit]]. And since <math>ud=\gcd(\gcd(a,b),c)</math> the result is proven. So if <math>na + mb = \gcd(a,b)</math> then there are <math>x</math> and <math>y</math> such that <math>x\gcd(a,b) + yc = \gcd(a,b,c)</math> so the final equation will be : <math>x(na + mb) + yc = (xn)a + (xm)b + yc = \gcd(a,b,c).\,</math> So then to apply to ''n'' numbers we use induction :<math>\gcd(a_1,a_2,\dots,a_n) =\gcd(a_1,\, \gcd(a_2,\, \gcd(a_3,\dots, \gcd(a_{n-1}\,,a_n))),\dots),</math> with the equations following directly. ==See also== * [[Euclidean domain]] * [[Linear congruence theorem]] * [[Kuṭṭaka]] == References == {{Reflist}} * {{Cite book |title=[[The Art of Computer Programming]] |author-link=Donald Knuth |first=Donald |last=Knuth |publisher=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== {{wikibooks|Algorithm Implementation|Mathematics/Extended Euclidean algorithm|Extended Euclidean algorithm}} * [http://mathforum.org/library/drmath/view/51675.html Source for the form of the algorithm used to determine the multiplicative inverse in GF(2^8)] {{number theoretic algorithms}} [[Category:Number theoretic algorithms]] [[Category:Articles with example pseudocode]] [[Category:Euclid]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Blue
(
edit
)
Template:Brown
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Cyan
(
edit
)
Template:Em
(
edit
)
Template:Green
(
edit
)
Template:Hatnote
(
edit
)
Template:ISBN
(
edit
)
Template:Magenta
(
edit
)
Template:Main
(
edit
)
Template:Math
(
edit
)
Template:Mvar
(
edit
)
Template:Nowrap
(
edit
)
Template:Number theoretic algorithms
(
edit
)
Template:Olive
(
edit
)
Template:Red
(
edit
)
Template:Reflist
(
edit
)
Template:See also
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Wikibooks
(
edit
)