Logo

MonoCalc

/

Extended Euclidean Algorithm

Math

Try:

Result for a = 240, b = 46

GCD(a, b)

2

x

-9

y

47

Verified
240 × -9 + 46 × 47 = 2, which matches gcd(a, b).

Forward pass — division steps

StepEquationRemainder
1240 = 5 × 46 + 1010
246 = 4 × 10 + 66
310 = 1 × 6 + 44
46 = 1 × 4 + 22
54 = 2 × 2 + 00

The algorithm stops at remainder 0, leaving GCD = 2.

Backward pass — back-substitution

StepCombination
12 = 1 × 2 + 0 × 0
22 = 0 × 4 + 1 × 2
32 = 1 × 6 + -1 × 4
42 = -1 × 10 + 2 × 6
52 = 2 × 46 + -9 × 10
62 = -9 × a + 47 × b

The final row gives the Bezout coefficients: x = -9, y = 47.

About This Tool

Extended Euclidean Algorithm Calculator – GCD and Bezout Coefficients

The Extended Euclidean Algorithm goes one step further than a plain GCD calculation. Given two integers a and b, it finds not only gcd(a, b) but also a pair of integers x and y — the Bezout coefficients — such that a×x + b×y = gcd(a, b). This identity, known as Bezout's identity, underlies modular inverses, RSA key generation, and the solution of linear Diophantine equations. This calculator shows every step of the process, from the initial division chain through the back-substitution that recovers x and y.

The Forward Pass: Euclidean Division

The algorithm starts exactly like the ordinary Euclidean algorithm. It divides a by b, records the quotient and remainder, then repeats the process on b and the remainder — dividend = quotient × divisor + remainder — until a remainder of zero is reached. The last nonzero remainder is the GCD. For a = 99, b = 78, the chain runs 99 = 1×78 + 21, 78 = 3×21 + 15, 21 = 1×15 + 6, 15 = 2×6 + 3, and finally 6 = 2×3 + 0, leaving gcd(99, 78) = 3.

The Backward Pass: Back-Substitution

Once the GCD is found, the calculator walks the same division chain in reverse. Starting from the equation that produced the GCD, it substitutes each prior equation upward, one remainder at a time, replacing two remainders with a single running combination of the numbers immediately above them. After working all the way back to the top, the combination is expressed purely in terms of the original a and b, yielding the Bezout coefficients directly. For the example above, this process recovers x = -11 and y = 14, since 99×(-11) + 78×14 = -1089 + 1092 = 3.

Bezout coefficients are not unique
If (x, y) satisfies a×x + b×y = gcd(a, b), then (x + (b/g)×t, y − (a/g)×t) is also a valid pair for any integer t, where g = gcd(a, b). The calculator returns the specific pair produced by standard back-substitution, the same result most textbooks and reference implementations report.

Solving Linear Diophantine Equations

A linear Diophantine equation asks for integer solutions to ax + by = c. A solution exists exactly when gcd(a, b) divides c evenly. When it does, scaling the Bezout coefficients by c / gcd(a, b) produces one particular solution: for 6x + 9y = 3, gcd(6, 9) = 3 divides 3, so a solution exists. When it does not — as with 4x + 6y = 5, where gcd(4, 6) = 2 does not divide 5 — the calculator reports that no integer solution exists rather than an approximate answer.

The General Solution Family

A single particular solution is only one member of an infinite family. Every integer solution to ax + by = c can be written as x = x₀ + (b/g)·t and y = y₀ − (a/g)·t for any integer t, where (x₀, y₀) is the particular solution and g = gcd(a, b). Enabling the general solution toggle displays this parametric formula alongside the particular solution, so you can generate as many valid pairs as you need.

Why This Matters: RSA and Modular Inverses

The Extended Euclidean Algorithm is the standard way to compute a modular multiplicative inverse: when gcd(a, m) = 1, the Bezout coefficient x in a×x + m×y = 1 satisfies a×x ≡ 1 (mod m), making x the inverse of a modulo m. This is a core step in RSA key generation, where the private exponent is the modular inverse of the public exponent, and it appears throughout number theory whenever a linear combination of two integers needs to be expressed explicitly.

Precision, Signs, and Limits

Every value is computed using arbitrary-precision BigInt arithmetic, so inputs with up to 300 digits are supported without the rounding errors that would eventually appear with ordinary floating-point numbers. Negative integers are handled automatically: the algorithm works internally with absolute values and adjusts the sign of the resulting coefficients so the identity still holds for your original, signed inputs. The only invalid input is a = 0 and b = 0 together, since their greatest common divisor is undefined.

Frequently Asked Questions

Is the Extended Euclidean Algorithm free?

Yes, Extended Euclidean Algorithm is totally free :)

Can I use the Extended Euclidean Algorithm offline?

Yes, you can install the webapp as PWA.

Is it safe to use Extended Euclidean Algorithm?

Yes, any data related to Extended Euclidean Algorithm only stored in your browser (if storage required). You can simply clear browser cache to clear all the stored data. We do not store any data on server.

How does this Extended Euclidean Algorithm calculator work?

It runs the Euclidean division chain forward on the two numbers you enter until the remainder reaches zero — that final divisor is the GCD. It then walks the same chain backward, back-substituting one step at a time, to recover the Bezout coefficients x and y such that a×x + b×y = gcd(a, b). Both the forward division steps and the backward back-substitution steps are shown in full.

What are Bezout coefficients used for?

Bezout coefficients x and y satisfy a×x + b×y = gcd(a, b) for any integers a and b. They are the foundation of computing modular multiplicative inverses (needed for RSA key generation and modular arithmetic), and they provide a particular solution whenever you need to solve a linear Diophantine equation ax + by = c.

How do I solve ax + by = c using this tool?

Switch to Diophantine equation mode and enter a, b, and c. The tool first checks whether gcd(a, b) divides c — a solution exists only when it does. If solvable, it scales the Bezout coefficients by c divided by the GCD to produce one particular solution, and can also display the full family of integer solutions using a parameter t.

Are the Bezout coefficients unique?

No. If (x, y) is one solution to a×x + b×y = gcd(a, b), then (x + (b/g)×t, y − (a/g)×t) is also a solution for any integer t, where g = gcd(a, b). This calculator returns the specific pair produced by the standard back-substitution process, which is the pair most textbooks and reference implementations report.

What happens if there is no integer solution to my Diophantine equation?

A linear Diophantine equation ax + by = c has an integer solution if and only if gcd(a, b) divides c evenly. If it does not, the calculator reports that no integer solution exists rather than showing a misleading approximate answer.

How large can the numbers be?

Every value is handled as an arbitrary-precision BigInt, so inputs with up to 300 digits are supported without any loss of precision — far beyond the range where ordinary floating-point numbers would start rounding incorrectly. Negative integers are also supported; the algorithm normalizes signs internally and adjusts the final coefficients to match your original inputs.