Inputs
Integers of up to 40 digits, with optional sign.
Examples
2
5520
−9
47
Integers of up to 40 digits, with optional sign.
2
5520
−9
47
The Euclidean algorithm computes the greatest common divisor (GCD) of two integers through successive divisions. It rests on a single observation: every common divisor of a and b also divides the remainder of dividing a by b. Since remainders strictly decrease, the process always terminates, and the last nonzero remainder is the GCD.
Algorithm invariant
The extended version of the algorithm does more than find the GCD: it walks through the same divisions while keeping track of how each remainder combines the original integers. The result is a pair of integer coefficients x and y that certify the GCD as a linear combination of a and b:
Bézout identity
The classic classroom presentation reconstructs this identity by back-substitution: the GCD is isolated from the last useful division and earlier remainders are replaced one by one. The tool shows that full derivation, together with the extended table (r, q, x, y) that performs the same computation forwards.
A linear Diophantine equation asks for integer solutions of ax + by = c. The solvability criterion is direct: a solution exists if and only if gcd(a, b) divides c. In that case, scaling the Bézout identity by c/g yields a particular solution, and every other solution is reached by sliding along the line:
Complete family of solutions
The congruence ax ≡ b (mod n) is the same Diophantine equation in disguise: ax − ny = b. With g = gcd(a, n), a solution exists if and only if g divides b; and when it does, there are exactly g solution classes modulo n, spaced n/g apart. The case g = 1 is the most important one: the solution is unique and is obtained by multiplying by the modular inverse a⁻¹, which is precisely the solution of ax ≡ 1 (mod n) and falls out of the extended algorithm for free.
Zero cases
gcd(a, 0) = |a| and gcd(0, 0) = 0 (standard convention); lcm(a, 0) = 0. The GCD is always reported as non-negative.
Canonical solutions
In the Diophantine mode, the particular solution is normalized to 0 ≤ x₀ < |b/g|; in the congruence mode, a and b are first reduced to the range [0, n). This makes results unique and reproducible.
Class enumeration
A congruence with g classes is fully enumerated up to g = 12; beyond that, the parametric form x ≡ x₀ (mod n/g), which describes all of them, is shown instead.
Lamé's theorem (1844) — regarded as the first complexity analysis of an algorithm — shows that the worst case of the Euclidean algorithm occurs with consecutive Fibonacci numbers: the number of divisions never exceeds five times the number of decimal digits of the smaller operand.
With the 40-digit limit of this tool, the longest possible derivation is around 190 divisions (for instance, two consecutive 40-digit Fibonacci numbers), all of which the table displays.