Extended Euclidean Algorithm Calculator - GCD

Fill this in to solve a linear Diophantine equation

240 & 46The textbook pair: gcd = 2 and 240(−9) + 46(47) = 2 in five divisions.

gcd(240, 46)

2

Bézout identity

240 × (-9) + 46 × 47 = 2

Products: -2,160 and 2,162, which differ by exactly 2. That single subtraction is the only check the answer needs.

Coefficient x

-9

multiplies a

Coefficient y

47

multiplies b

Divisions used

5

Lamé allows up to 10

lcm(a, b)

5,520

|a × b| ÷ gcd, free from the same run

Every division, with the two coefficient columns carried alongside — the highlighted row holds the gcd

iDivisionQuotient qRemainder rs (× |a|)t (× |b|)
0start: |a|24010
1start: |b|4601
2240 = 5 × 46 + 105101-5
346 = 4 × 10 + 646-421
410 = 1 × 6 + 4145-26
56 = 1 × 4 + 212-947
64 = 2 × 2 + 02023-120

Each new row subtracts q copies of the row above from the row two above — the same recurrence drives the remainder, the s column and the t column, which is why the coefficients cost nothing extra.

Every integer solution of 240x + 46y = 2

x = -9 + k × 23

y = 47 − k × 120

The steps are b ÷ gcd and a ÷ gcd, so a change of one in k moves x and y in opposite directions by exactly the amount that keeps the total fixed.

k = 0

240 × (-9) + 46 × 47 = 2

kxyax + by
-2-552872
-1-321672
0-9472
114-732
237-1932

Right-hand column never moves: every row hits 2 exactly. The pair the algorithm hands back is only the k = 0 entry of an infinite list, which is why two correct calculators can print different coefficients for the same inputs.

Smallest non-negative x: x = 14 with y = -73 (at k = 1). The x values repeat with period 23, so this is the one you want whenever x counts physical objects — coins, packets, tiles — and cannot go negative. Note y is still negative here, so no solution with both values non-negative exists at this x.

The quotient column read as a continued fraction for |a| ÷ |b|

240 ÷ 46 = [5; 4, 1, 1, 2]

5/121/426/547/9120/23

The highlighted convergent 47/9 is not decoration — its denominator is |x| = 9 and its numerator is |y| = 47. The Bézout coefficients are the second-to-last convergent of a ÷ b, always.

How this run compares with the provable limits

MeasureDivisionsWhere it comes from
This run5counted from the table above
Typical for this size50.843 × ln(smaller input) + 1.47, the average over random pairs
Tight worst case8hit only by 55 and 34, consecutive Fibonacci numbers
Lamé’s bound105 × the 2 decimal digits of 46

Doubling the size of the inputs adds a constant handful of divisions, never doubles the work — that logarithmic growth is what makes the extended Euclidean algorithm usable on 2048-bit numbers where trial division would never finish.

How to Use This Calculator

  1. Type your two integers into First integer (a) and Second integer (b). Negatives are accepted — the table runs on the magnitudes and the sign is reattached to the matching coefficient.
  2. Read the green identity strip. It shows a × x + b × y = gcd with your actual numbers, so you can verify the result with one subtraction.
  3. Work down the step table. The q column is the quotient of each division, and the s and t columns are the coefficients being built up row by row.
  4. To solve a linear Diophantine equation, put your target in the Optional target (c) field. The verdict box says immediately whether gcd(a, b) divides c, and scales the coefficients when it does.
  5. Step k up and down to walk the solution family. The right-hand column of that table never changes, which is the fastest way to see that the coefficients are not unique.

Share this calculator

Help others solve their calculations

Found this calculator helpful? Share it with your friends, students, or colleagues who might need it!

Extended Euclidean Algorithm Calculator: Reading GCD and Bézout Coefficients Off One Table

About the Author

Marko Šinko - Co-Founder & Lead Developer

Marko Šinko

Co-Founder & Lead Developer, AI Math Calculator

Lepoglava, Croatia
Advanced Algorithm Expert

Croatian developer with a Computer Science degree from University of Zagreb and expertise in advanced algorithms. Co-founder of award-winning projects, ensuring precise mathematical computations and reliable calculator tools.

📅 Published:
Extended Euclidean Algorithm Calculator showing a shrinking remainder staircase with arrows curving back to the Bezout coefficients x and y

An extended Euclidean algorithm calculator gives you three numbers instead of one. Feed it 240 and 46 and it returns gcd(240, 46) = 2, plus the pair x = −9 and y = 47 that makes 240(−9) + 46(47) = 2 true. Check it by hand: −2,160 + 2,162 = 2. The plain Euclidean algorithm would have stopped at the 2.

Those two extra numbers are the whole point. They are what turn a gcd into a modular inverse, an RSA private exponent, or a solution to 6x + 15y = 21. Below: what the extra columns actually compute, a sign pattern that catches arithmetic slips for free, why the coefficients your calculator prints may differ from your textbook’s and both still be right, how to aim the identity at any target c, and the two facts about running time that most treatments skip.

What the Two Extra Columns Carry

Ordinary Euclid keeps one column: the remainders, shrinking until one of them is 0. The extended version keeps three, and the two new ones answer a different question — not “what is left over?” but “how do I rebuild this remainder out of the two numbers I started with?”

Every row of the table stands for one true statement of the form r = s·a + t·b. Row 0 is a itself, so it starts at s = 1, t = 0. Row 1 is b, so it starts at s = 0, t = 1. After that each row is built from the two above it using the quotient q of that division, and the same recurrence applies to all three columns at once:

r(i) = r(i−2) − q × r(i−1)

s(i) = s(i−2) − q × s(i−1)

t(i) = t(i−2) − q × t(i−1)

That is why the coefficients are effectively free. You are not running a second algorithm; you are applying the identical subtraction to two more columns while the remainders do what they were going to do anyway. When the remainder finally hits 0, the row above it holds the gcd — and the s and t sitting on that row are your answer. Compare this with the plain greatest common factor calculator, which runs the same divisions and throws the bookkeeping away.

The Signs Alternate, and That’s a Free Error Check

Here is the s and t column for 240 and 46, in order:

DivisionRemainders (× 240)t (× 46)
start24010
start4601
240 = 5 × 46 + 10101−5
46 = 4 × 10 + 66−421
10 = 1 × 6 + 445−26
6 = 1 × 4 + 22−947

Look down the s column: 1, 0, 1, −4, 5, −9. Now the t column: 0, 1, −5, 21, −26, 47. From the third row on, both columns flip sign on every single step, and the magnitudes never shrink. That isn’t a coincidence of this example — it holds for every input, and it falls straight out of the recurrence, because each row subtracts a positive multiple of a value with the opposite sign.

Use it. If you are working the table by hand and two consecutive s values share a sign, or a magnitude goes down instead of up, you have an arithmetic error in that row and you can stop and fix it rather than discovering it six rows later. The two columns also stay opposite to each other throughout, which is the second half of the same check.

Why Your Answer and the Textbook’s Answer Both Check Out

Students lose a lot of time to this: the calculator says x = −9, the worked solution in the book says x = 14, and neither is wrong. The Bézout coefficients are not unique. If (x₀, y₀) works, so does every pair of the form

x = x₀ + k × (b ÷ g)

y = y₀ − k × (a ÷ g)

for any integer k, where g is the gcd. The reason is short: you have added k(ab/g) to one product and subtracted the same k(ab/g) from the other, so the total cannot move. With a = 240, b = 46 and g = 2 the steps are 46 ÷ 2 = 23 and 240 ÷ 2 = 120:

kxy240x + 46y
−1−32167−7,680 + 7,682 = 2
0−947−2,160 + 2,162 = 2
114−733,360 − 3,358 = 2
237−1938,880 − 8,878 = 2

The algorithm hands back one row of that infinite list — conventionally the one with the smallest magnitudes, which is what makes it the useful starting point. Which row you actually want depends on the job. Modular arithmetic wants the representative in the range 0 to n − 1, so you add the modulus until the coefficient is positive; that single adjustment is the whole difference between −367 and 2753 in the RSA example the modular inverse calculator walks through. Counting problems want the smallest non-negative x, which the k-stepper above finds directly.

Hitting a Target That Isn’t the GCD

Linear Diophantine equations ask for integer solutions to ax + by = c, and the extended Euclidean algorithm settles them completely in two moves. First, the existence test: a solution exists exactly when gcd(a, b) divides c. Nothing else matters. No search, no case analysis. Every value of ax + by is a multiple of the gcd, so a target that isn’t a multiple is simply unreachable.

Second, if it does divide, scale. Run the algorithm to get ax₀ + by₀ = g, then multiply both coefficients by c ÷ g. Take 240x + 46y = 18. Since g = 2 divides 18, multiply x₀ = −9 and y₀ = 47 by 9: x = −81, y = 423. Check: 240(−81) = −19,440 and 46(423) = 19,458, and 19,458 − 19,440 = 18. Try 240x + 46y = 7 instead and there is nothing to compute — the left side is always even and 7 isn’t.

The everyday version is a coin or packaging problem. Suppose you sell items in 6-unit and 15-unit packs and a customer wants exactly 21 units. gcd(6, 15) = 3, and 3 divides 21, so it is possible. The algorithm gives 6(−2) + 15(1) = 3; scaling by 7 gives x = −14, y = 7. A negative pack count is meaningless, so step k until x turns non-negative: the family is x = −14 + 5k, y = 7 − 2k, and k = 3 gives x = 1, y = 1. One pack of each. Notice how the divisibility test did the real work — this is the same criterion the divisibility calculator checks, applied to c and the gcd.

One warning about scaling. Multiplying x₀ and y₀ by c ÷ g inflates the coefficients, and for a large c that inflation is real: with c = 10⁶ the coefficients above become x = −4,500,000 and y = 23,500,000. They are still correct, but if the answer needs to be small, reduce with the k family afterwards rather than accepting whatever the scaling produced.

Lamé’s Theorem: The Slowest Inputs Are Always Fibonacci

In 1844 Gabriel Lamé proved the first non-trivial running-time bound in the history of algorithms: the Euclidean algorithm needs at most five times as many divisions as the smaller number has decimal digits. Two-digit inputs never take more than 10 steps. Four-digit inputs never take more than 20.

What makes the result memorable is which inputs actually reach the bound. They are always consecutive Fibonacci numbers, because those are precisely the pairs where every quotient comes out as 1 — the division removes as little as it possibly can, so the remainders shrink as slowly as arithmetic allows.

Worst-case pairDigits in the smallerDivisions takenLamé’s bound
13, 8155
89, 552910
987, 61031415
10946, 676541920

The bound is close but never tight past a single digit, and real inputs are nowhere near it. The average number of divisions over random pairs grows like 0.843 × ln(n) + 1.47, which for four-digit numbers is about 9 — less than half the worst case, and the gap widens as the numbers grow. Run 240 and 46 through the calculator and you get 5 divisions against a bound of 10.

There is a small bonus in the Fibonacci case that is worth seeing once. Run the algorithm on 2584 and 1597 and the coefficients come back as 610 and −987 — Fibonacci numbers themselves, four places down the sequence. The structure is that self-similar. If you want to generate the pairs to test this, the Fibonacci calculator lists them directly.

The Quotient Column Is a Continued Fraction in Disguise

Take the quotients from the 240 and 46 run in order — 5, 4, 1, 1, 2 — and write them as a continued fraction. You get 240/46 = [5; 4, 1, 1, 2], meaning 5 + 1/(4 + 1/(1 + 1/(1 + 1/2))), which evaluates to 5.2173913…, exactly 240 ÷ 46. This is not a curiosity bolted on afterwards. The Euclidean algorithm is the continued fraction expansion; the quotients are the expansion’s terms by definition.

The payoff comes from the convergents — the fractions you get by truncating the expansion early. For [5; 4, 1, 1, 2] they are 5/1, 21/4, 26/5, 47/9, and 120/23. The last one is 240/46 in lowest terms, which is expected. The interesting one is the second-to-last: 47/9. Compare it with the coefficients the algorithm produced, x = −9 and y = 47. The denominator is |x| and the numerator is |y|, and that correspondence holds for every input, not just this one.

This is why the Bézout coefficients are as small as they are — they are the numerator and denominator of the best rational approximation to a/b with a denominator below b/g, which is a genuinely strong optimality property. It also gives you a second, independent way to compute them: build the convergents forward from the quotients and read off the penultimate one. The calculator above shows both, and highlights the convergent that matches. For the plain remainder arithmetic driving every quotient in that list, the modulo calculator is the simpler tool.

Zeros, Negatives, and the Cases an Extended Euclidean Algorithm Calculator Must Handle

Four inputs break naive code, and all four have clean answers:

  • b = 0. gcd(a, 0) = |a|, and the identity is a(1) + 0(0) = |a| for positive a. The table never runs a division; row 0 is already the answer. Any implementation that divides before testing crashes here.
  • Both zero. gcd(0, 0) = 0 by convention, and there is no meaningful coefficient pair — 0x + 0y is 0 whatever you choose. Treat it as a special case rather than a bug.
  • Negative inputs. The gcd is defined on magnitudes, so gcd(−1234, 4321) = gcd(1234, 4321) = 1. Run the table on |a| and |b|, then flip the sign of x if a was negative and of y if b was negative. The identity survives untouched, because you flipped both the number and its multiplier.
  • a smaller than b. Nothing special happens: the first quotient comes out as 0 and that row simply swaps the two values. It costs one wasted division and no library bothers to pre-sort for it. You can see it live by entering 17 and 3120.

Beyond those, the one substantive mistake is treating a coefficient as if it were a residue. The x that comes out of the table can be large and negative — for 17 and 3120 it is −367 — and it is a perfectly correct Bézout coefficient in that form. It only becomes a modular inverse after you reduce it into range. Mixing the two conventions is what produces answers that verify in one context and fail in another; the standard formulation of the algorithm returns the raw coefficient and leaves the reduction to you.

Once you are comfortable with the identity, most of number theory opens up from it. Coprimality becomes a one-line test, modular inverses become a sign fix-up, the Chinese Remainder Theorem calculator merges congruences by calling this algorithm once per pair, and lcm(a, b) = |ab| ÷ gcd(a, b) drops out of the same run — the least common multiple calculator is doing exactly that underneath. One table, five divisions, and a surprising amount of arithmetic falls out of it.

Frequently Asked Questions

Still Have Questions?

The detailed content on this page provides comprehensive explanations and examples to help you understand better.