Home › Calculators › Extended Euclidean Algorithm Calculator

Extended Euclidean Algorithm Calculator

Enter two integers to get their greatest common divisor and the Bézout coefficients x and y with a·x + b·y = gcd(a, b). Every division, quotient and coefficient is shown in a table you can follow by hand or download.

Last updated:

Whole number, can be negative or very large

Leave blank to skip the linear Diophantine equation

Enter your values to see the result.
Advertisement
Advertisement

How to use the Extended Euclidean Algorithm Calculator

  1. Enter a and b, the two integers you want to work with. Commas and spaces in long numbers are ignored.
  2. Optionally enter c to solve the equation a·x + b·y = c in whole numbers.
  3. Press Calculate (results also update as you type) to see the gcd, x, y and the check a·x + b·y = gcd.
  4. Follow the table: each row shows the quotient, the remainder and the running coefficients s and t.
  5. Copy or download the answer, or the step table as a CSV file for homework or notes.

How the extended Euclidean algorithm works

The Euclidean algorithm rests on one fact: gcd(a, b) = gcd(b, a mod b). You divide the larger number by the smaller, keep the remainder, and repeat with the divisor and the remainder until the remainder is 0. The last non-zero remainder is the gcd.

The extended version also writes every remainder as a combination of the original numbers, ri = si·a + ti·b. It starts with two rows, r0 = a (s = 1, t = 0) and r1 = b (s = 0, t = 1), and then applies the same update to all three columns:

  • qi = ⌊ri−1 ÷ ri⌋
  • ri+1 = ri−1 − qi·ri,   si+1 = si−1 − qi·si,   ti+1 = ti−1 − qi·ti

When the remainder reaches 0, the row above it holds the gcd together with its coefficients x = s and y = t.

Worked example: gcd(240, 46)

iDivisionqrst
0start24010
1start54601
2240 = 5·46 + 104101−5
346 = 4·10 + 616−421
410 = 1·6 + 4145−26
56 = 1·4 + 222−947
64 = 2·2 + 0023−120

The last non-zero remainder is 2, so gcd(240, 46) = 2 with x = −9 and y = 47. Check: 240 × (−9) + 46 × 47 = −2,160 + 2,162 = 2. The final row (23, −120) is a useful by-product: 240 × 23 − 46 × 120 = 0, and 23 = 46/2 and 120 = 240/2 are the steps of the general solution.

Where it is used

  • Modular inverses. If gcd(a, m) = 1, then a·x ≡ 1 (mod m). This is how the private exponent is computed in RSA key generation.
  • Linear Diophantine equations. a·x + b·y = c has whole-number solutions only when gcd(a, b) divides c; the coefficients give one solution directly.
  • Chinese remainder theorem. Combining congruences such as x ≡ 2 (mod 3) and x ≡ 3 (mod 5) needs inverses of one modulus modulo the other.
  • Fractions and ratios. Dividing by the gcd reduces a fraction to lowest terms.

Tip: the coefficients this method returns are the "minimal" ones, with |x| ≤ b/(2g) and |y| ≤ a/(2g) in most cases, so they rarely grow larger than the inputs.

Frequently asked questions

What does the extended Euclidean algorithm do?

It finds the greatest common divisor of two integers a and b and, at the same time, two integers x and y such that a·x + b·y = gcd(a, b). The ordinary Euclidean algorithm only gives the gcd; the extended version keeps track of how each remainder is built from a and b.

What are Bézout coefficients?

They are the integers x and y in Bézout's identity a·x + b·y = gcd(a, b). They always exist when a and b are not both zero, but they are not unique: if (x, y) works, so does (x + k·b/g, y − k·a/g) for any integer k, where g is the gcd.

How do I find a modular inverse with this calculator?

Enter the number as a and the modulus as b. If gcd(a, b) = 1, the coefficient x satisfies a·x ≡ 1 (mod b), so x reduced modulo b is the inverse. For example, the inverse of 17 modulo 3120 is 2753, because 17 × 2753 = 46,801 = 15 × 3120 + 1. If the gcd is larger than 1, no inverse exists.

Why is one of my coefficients negative?

For positive a and b, one of x and y is almost always negative (or zero), because a·x + b·y has to come out as small as the gcd. That is expected. Adding b/g to x and subtracting a/g from y gives another valid pair if you prefer a positive x.

When does a·x + b·y = c have integer solutions?

Exactly when gcd(a, b) divides c. Then you multiply the Bézout coefficients by c / gcd(a, b) to get one solution, and the general solution adds multiples of b/g to x and subtracts the same multiples of a/g from y. If the gcd does not divide c, there are no whole-number solutions.

How many steps does the Euclidean algorithm take?

Very few. By Lamé's theorem the number of division steps is at most about five times the number of digits of the smaller number. The worst case is two consecutive Fibonacci numbers, such as 89 and 55, where every quotient is 1.

Can I use negative numbers or huge numbers?

Yes. The calculator works with exact integer arithmetic of any length up to 400 digits. With negative inputs it runs the algorithm on the absolute values and then flips the sign of the matching coefficient, so a·x + b·y = gcd still holds. The gcd itself is always reported as a non-negative number.

Are all the tools on FreeToolCabin tested for quality and accuracy?

Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.

Related tools

GCF and LCM CalculatorFind the GCF and LCM of two or more numbers with Euclid steps and prime factorization.Modulo CalculatorCalculate a mod n and the quotient, with floored, truncated and Euclidean rules.Prime Factorization CalculatorFind prime factors in exponent form with a factor tree, divisor count and divisors list.Set CalculatorUnion, intersection, difference and complement of up to 4 sets, with a Venn diagram and counts.Graphing CalculatorPlot multiple functions with zoom, pan, intersections and a value table.Exponent CalculatorRaise any number to a power, including negative and fractional exponents.Logarithm CalculatorCalculate log in any base, ln, log10, log2 and antilog with change-of-base steps.Quadratic Equation SolverSolve ax² + bx + c = 0 with exact roots, steps, vertex and a parabola graph.Matrix CalculatorAdd, multiply, invert and row-reduce matrices up to 5×5 with exact fractions and steps.Prime Number CheckerCheck if a number is prime, factor it with a factor tree and list primes in a range.Cubic Yard CalculatorConvert length, width and depth in any unit to cubic yards, cubic feet, m³ and material weight.Factorial CalculatorExact n! up to 5000! with digit count, trailing zeros and double factorial.Exponential Growth CalculatorExponential growth and decay: final amount, rate or time, discrete or continuous.Linear Regression CalculatorLeast-squares line of best fit: slope, intercept, r, r², predictions and a scatter plot.System of Equations SolverSolve 2×2, 3×3 and 4×4 linear systems with exact fractions and step-by-step working.Cubic Equation SolverSolve ax³ + bx² + cx + d = 0 for real and complex roots with steps and a graph.Summation CalculatorEvaluate sigma notation: sum any expression in n from a to b with partial sums and a chart.Arithmetic Sequence CalculatorFind the n-th term, sum and full list of an arithmetic sequence, or d from two terms.Geometric Sequence CalculatorFind the n-th term, sum and infinite sum of a geometric sequence, or r from two terms.Fibonacci CalculatorExact n-th Fibonacci number (BigInt), sequence lists, Fibonacci check and golden ratio.Factors CalculatorAll factors and factor pairs of a number, prime factors, divisor count and perfect check.Depth of Field CalculatorNear and far focus limits, depth of field and hyperfocal distance for any camera or lens.Crop Factor CalculatorCrop factor, full-frame equivalent focal length and aperture, and field of view for any sensor.Completing the Square CalculatorRewrite ax² + bx + c in vertex form a(x − h)² + k step by step and solve it.Derivative CalculatorSymbolic derivative of any f(x) up to 5th order, with steps, slope at a point and a graph.Exposure Value CalculatorEV from aperture, shutter and ISO, or solve any missing setting with sunny 16 presets.Factoring Trinomials CalculatorFactor trinomials and polynomials: GCF, ac method, difference of squares, cubes, with steps.Integral CalculatorDefinite integral of f(x) from a to b with adaptive Simpson, area graph and antiderivative.Least Common Denominator CalculatorFind the LCD of two or more fractions and rewrite them as equivalent fractions, with steps.Limiting Reactant CalculatorFind the limiting reactant, theoretical yield and leftover excess from an equation.Parabola CalculatorVertex, focus, directrix, axis, intercepts and graph of a parabola from its equation.Polynomial Root FinderFind all real and complex roots of a polynomial up to degree 10, with the factored form.Prime Number List GeneratorList all primes in a range or the first N primes with a fast sieve; copy or download the list.Quadratic Regression CalculatorFit y = ax² + bx + c (or up to degree 6) to x, y data with R², predictions and a graph.Scatter Plot MakerPaste X,Y pairs for a scatter plot with trendline, equation, r and R². Download PNG.Synthetic Division CalculatorDivide polynomials by x − c (synthetic table) or any polynomial (long division) with steps.3D Graphing CalculatorPlot z = f(x, y) surfaces and 3D parametric curves; rotate, zoom, add contours, save PNG.Algebra CalculatorSimplify, expand and factor algebraic expressions with exact answers and step-by-step working.Inverse Function CalculatorFind the inverse f⁻¹(x) of a function with steps, domain restrictions and a graph.Laplace Transform CalculatorLaplace transform of f(t) with the table rule used for each term and the region of convergence.Limit CalculatorEvaluate limits at a point or at infinity, one-sided or two-sided, with steps, table and graph.Linear Interpolation CalculatorInterpolate or extrapolate y for any x from two points or a data table, with steps and a plot.Matrix Inverse CalculatorFind the inverse of a 2×2 to 8×8 matrix in exact fractions with Gauss–Jordan or adjugate steps.Partial Derivative CalculatorPartial derivatives ∂/∂x, ∂/∂y, second and mixed orders with steps, gradient and Hessian.RREF CalculatorRow reduce any matrix to RREF with exact fractions, every row operation, pivots and rank.Stem and Leaf Plot MakerMake stem-and-leaf plots (standard, split stems or back-to-back) with a key; copy or save PNG.Taylor Series CalculatorTaylor and Maclaurin polynomials of any f(x) to order n, with exact coefficients and a graph.Antilog CalculatorInverse logarithm (antilog) in base 10, e, 2 or any base, with steps and scientific notation.Asymptote CalculatorFind vertical, horizontal and slant asymptotes and holes of a function, with graph and reasoning.Cramer's Rule CalculatorSolve 2x2 and 3x3 linear systems by Cramer's rule with every determinant shown step by step.Dot Plot MakerMake a dot plot or line plot from data or a frequency table; download PNG or SVG.Exponential Regression CalculatorFit y = a·bˣ or y = a·e^(kx) to data with r², predictions, residuals and a plot.Factor Tree GeneratorDraw the prime factor tree of any number as a printable diagram, with exponent form and steps.Function Composition CalculatorFind f(g(x)), g(f(x)), f + g, f · g and f / g with values at a point and domains.X and Y Intercept CalculatorFind the x- and y-intercepts of lines, curves and functions with steps and a graph.Line Intersection CalculatorFind where two lines cross from equations, points or slopes, with parallel checks and a graph.Linear Programming SolverSolve max/min linear programs with the simplex method: tableau steps and a 2-variable graph.Modular Exponentiation CalculatorCompute a^b mod m for huge numbers with square-and-multiply steps (BigInt).Modular Inverse CalculatorFind a⁻¹ mod m with the extended Euclidean algorithm, step by step, for huge numbers too.Nth Term CalculatorFind the nth term formula of a sequence: arithmetic, geometric, quadratic or cubic.Partial Fraction Decomposition CalculatorSplit a rational function into partial fractions with the setup, equations and solution.Perpendicular Line CalculatorEquation of the perpendicular and parallel line through a point, with distance and a graph.Polar Graphing CalculatorGraph polar equations r = f(θ): roses, cardioids, spirals, with a θ range slider and PNG export.Polynomial CalculatorAdd, subtract, multiply, divide and evaluate polynomials; see roots, degree and a graph.Polynomial Long Division CalculatorDivide polynomials with the full long division layout, quotient, remainder and steps.Scale Factor CalculatorScale factor between similar figures, plus scaled lengths, areas (k²) and volumes (k³).Table of Values CalculatorMake an x/y table for any function with a start, end and step, then plot it or copy CSV.Determinant CalculatorDeterminant of a 2×2 to 8×8 matrix with exact fractions and cofactor or row-reduction steps.
See all 74 Algebra & Calculus Calculators →