ArticleslgStudy

computer science

Extended Euclidean algorithm

Extended Euclidean algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Extended Euclidean algorithm rather than just read about it. In short: 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 ax+by=gcd⁡(a,b); it is generally denoted as xgcd⁡(a,b). This is a certifying algorithm, because the gcd is the only number that can simul…

Extended Euclidean algorithm — main illustration
Extended Euclidean algorithm — illustration

Key takeaways

  • Extended Euclidean algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Extended Euclidean algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Extended Euclidean algorithm from memory before moving on to harder problems.

Reference excerpt

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 ax+by=gcd⁡(a,b); it is generally denoted as xgcd⁡(a,b). This is a certifying algorithm, because the gcd is the only number that can simultaneously satisfy this equation and divide the inputs. 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 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 q1,…,qk of quotients and a sequence r0,…,rk+1 of remainders such that

r0=ar1=b⋮ri+1=ri−1−qiriand0≤ri+1<|ri|(this defines qi)⋮ It is the main property of Euclidean division that the inequalities on the right define uniquely qi and ri+1 from ri−1 and ri. The computation stops when one reaches a remainder rk+1 which is zero; the greatest common divisor is then the last nonzero remainder rk. The extended Euclidean algorithm proceeds similarly, but adds two other sequences, as follows

r0=ar1=bs0=1s1=0t0=0t1=1⋮⋮ri+1=ri−1−qiriand 0≤ri+1<|ri|(this defines qi)si+1=si−1−qisiti+1=ti−1−qiti⋮ The computation also stops when rk+1=0 and gives

rk is the greatest common divisor of the input a=r0 and b=r1. The Bézout coefficients are sk and tk, that is gcd⁡(a,b)=rk=ask+btk The quotients of a and b by their greatest common divisor are given by sk+1=±bgcd⁡(a,b) and tk+1=∓agcd⁡(a,b) (the sign ∓ is opposite to ±). Moreover, if a and b are both positive and gcd⁡(a,b)≠min⁡(a,b), then

|si|≤⌊b2gcd⁡(a,b)⌋and|ti|≤⌊a2gcd⁡(a,b)⌋ for 0≤i≤k, where ⌊x⌋ denotes the integral part of x, that is the greatest integer not greater than x. Together with the general form of all Bézout coefficient pairs, these inequalities imply that the extended Euclidean algorithm provides the unique pair that simultaneously minimizes the absolute values of both coefficients, called the minimal Bézout pair. It also means that if a and b fit into an unsigned integer data type, a computer program can compute the Bézout coefficients in the corresponding signed integer type without integer overflow.

Examples 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.

The following example uses a more compact notation. The greatest common divisor of a=68 and b=30 can be calculated like this:

g=gcd(68,30)↑−−(−2)=gcd(8,30)(−3)−−↑ =gcd(8,6)↑−−(−1)=gcd(2,6)(−3)−−↑=gcd(2,0)=2, where ↑−−(−2) in the first step means that −2 times 30 is added to 68 (the gcd does not change when adding a multiple of one number to the other).

Applying the green-indicated additions of multiples analogously to equations, starting with a=1a+0b and b=0a+1b, leads to g=4a−9b, according to the adjacent calculations (the corresponding table far right uses row operations).

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Extended Euclidean algorithm

Start with the simplest possible case. Write down what Extended Euclidean algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Extended Euclidean algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Extended Euclidean algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Extended Euclidean algorithm

In research
Extended Euclidean algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Extended Euclidean algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Extended Euclidean algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Euclid, Number theoretic algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Extended Euclidean algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Extended Euclidean algorithm” →

How to study Extended Euclidean algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Extended Euclidean algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Extended Euclidean algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Extended Euclidean algorithm in simple terms?

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 ax+by=gcd⁡(a,b)…

Why does Extended Euclidean algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Extended Euclidean algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Extended Euclidean algorithm.

Tags

  • Euclid
  • Number theoretic algorithms

Keep exploring