TOPICS
Search

Extended Euclidean Algorithm


The extended Euclidean algorithm augments the Euclidean algorithm for integers a and b so that, in addition to g=GCD(a,b), it computes integers s and t satisfying Bézout's identity

 g=sa+tb.

For a>=b>0, start with (r_0,s_0,t_0)=(a,1,0) and (r_1,s_1,t_1)=(b,0,1). Let q_i=|_(r_(i-1))/(r_i)_|, where |_x_| denotes the floor function, and set

 (r_(i+1),s_(i+1),t_(i+1))=(r_(i-1),s_(i-1),t_(i-1))-q_i(r_i,s_i,t_i).

When r_(k+1)=0, the algorithm returns r_k=GCD(a,b) together with s_k and t_k. For example, it gives GCD(252,198)=18 together with 18=4(252)-5(198).

The extended Euclidean algorithm is used to solve linear Diophantine equations and to compute multiplicative inverses modulo an integer.


See also

Bézout's Identity, Euclidean Algorithm, Extended Greatest Common Divisor, Greatest Common Divisor, Modular Inverse

Explore with Wolfram|Alpha

References

Knuth, D. E. The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 3rd ed. Reading, MA: Addison-Wesley, 1998.

Cite this as:

Weisstein, Eric W. "Extended Euclidean Algorithm." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/ExtendedEuclideanAlgorithm.html

Subject classifications