Extended Euclidean Algorithm Calculator

Find the gcd of two integers and the Bezout coefficients that express it as a linear combination.

Math and PercentUpdated 2026-11-192 min read
Extended Euclidean Algorithm Calculator
A whole number between minus 10 to the 12th power and 10 to the 12th power.
A whole number in the same range.
Greatest common divisor
...
Bezout coefficient x...
Bezout coefficient y...
a times x plus b times y...
Bezout identity...

What it does

The Euclidean algorithm finds the greatest common divisor of two integers by repeated division. The extended version also tracks how each remainder is built from the original numbers, which produces two integers called Bezout coefficients.

This tool takes the two integers and returns the gcd together with coefficients x and y such that a times x plus b times y equals that gcd. A check row shows the computed combination so you can verify the identity immediately.

The page opens with a = 240 and b = 46. The gcd is 2, and one valid pair is x = minus 9 and y = 47, because 240 times minus 9 plus 46 times 47 equals 2.

The algorithm keeps a running remainder and two coefficient pairs, updating them at each division step. A guard limits the loop so hostile or unusual inputs cannot cause endless work.

When the gcd is 1 the coefficient x is the multiplicative inverse of a modulo b, up to sign. That is the main reason the extended algorithm matters in modular arithmetic and cryptography.

How to use

  1. Enter the first integer a.
  2. Enter the second integer b.
  3. Read the gcd as the main result.
  4. Check the Bezout coefficients x and y.
  5. Verify the identity using the check row.
  6. Try coprime numbers such as 35 and 27 to see a gcd of 1.

Formula

At each step, replace (a, b) with (b, a mod b) while updating coefficients so that a times x plus b times y stays equal to the current remainder
a, b
The two input integers.
gcd
The greatest common divisor, always nonnegative.
x, y
Integers satisfying a times x plus b times y equals the gcd.

Starting with x = 1, y = 0 for a and x = 0, y = 1 for b, each division step subtracts the quotient times the current coefficient pair from the previous pair. When the remainder reaches zero, the previous remainder is the gcd and the previous coefficients give the identity.

Worked example

Example: 240 and 46

The page opens with a = 240 and b = 46.

  1. Divide 240 by 46 to get quotient 5 and remainder 10.
  2. Divide 46 by 10 to get quotient 4 and remainder 6.
  3. Divide 10 by 6 to get quotient 1 and remainder 4.
  4. Divide 6 by 4 to get quotient 1 and remainder 2.
  5. Divide 4 by 2 to get quotient 2 and remainder 0, so the gcd is 2.
  6. Back substituting gives x = minus 9 and y = 47.
  7. Check: 240 times minus 9 plus 46 times 47 is minus 2,160 plus 2,162, which is 2.

Reference table

Bezout identities for sample integer pairs
abgcdxy
240462-947
35271-1013
99783-1114

Limits

  • Inputs must be whole numbers between minus 10 to the 12th power and 10 to the 12th power.
  • At least one input must be nonzero.
  • The returned coefficients are one valid pair, not the only pair.
  • The loop is capped at 200 iterations.

Common mistakes

  • Thinking the coefficients are unique.
  • Forgetting that the gcd is reported as a positive number.
  • Confusing the Euclidean and extended Euclidean outputs.
  • Using the coefficient y as a modular inverse without reducing modulo b.
  • Entering non integer inputs.

Try it yourself

Quick checks. Answers sit tucked under each question.

Find gcd and coefficients for 35 and 27.

gcd 1, with x = minus 10 and y = 13 because 35 times minus 10 plus 27 times 13 equals 1.

Find gcd and coefficients for 99 and 78.

gcd 3, with x = minus 11 and y = 14.

What do coprime inputs give?

A gcd of 1 and a linear combination equal to 1.

Questions

What are Bezout coefficients?

Integers x and y such that a times x plus b times y equals the gcd of a and b.

Are the coefficients unique?

No. Adding multiples of b divided by the gcd to x and corresponding multiples to y gives another pair.

Why is this useful?

It gives modular inverses when the inputs are coprime and helps solve linear Diophantine equations.

Can inputs be negative?

Yes. The gcd is reported positive and the coefficient signs are adjusted.

What if both inputs are zero?

The gcd is undefined and the tool reports an error.

How is the result checked?

Multiply a by x and b by y and add. The check row shows that the total equals the gcd.

Sources

How this page was checked. The formula above was matched against the cited sources, and the worked example was run through the tool on this page before publishing. If you spot an error, contact us so we can fix it and note the correction.