b2KIT

Euclidean Algorithm Visualizer

Find GCD using the Euclidean algorithm with visual rectangle decomposition. Shows extended GCD and Bezout coefficients.

Tested tool guide Tested browser tools Checked August 16, 2026

What Euclidean Algorithm Visualizer does, with a checked example

The Euclidean Algorithm Visualizer takes two integers and runs the repeated-division process from Euclid's Elements: divide, keep the remainder, repeat, and the last nonzero remainder is the greatest common divisor (GCD). Each step is drawn as squares filling a rectangle of the two inputs, so the GCD appears as the largest square that tiles it exactly. An extended run also returns Bezout coefficients x and y with ax + by = gcd(a,b). What surprises people: the GCD is not the product of common prime factors you would spot by inspection. The process is purely mechanical, works for any pair, and the Bezout identity is a byproduct of the same steps.

Worked example

A concrete input and expected output from the current implementation.

Input

252 and 105

Expected output

GCD: 21

Steps:
252 = 2 x 105 + 42
105 = 2 x 42 + 21
42 = 2 x 21 + 0

Bezout identity: 21 = -2 x 252 + 5 x 105

21 is the last nonzero remainder, so it is the GCD. Back-substituting the remainders rewrites 21 as -2(252) + 5(105), and -504 + 525 = 21 verifies the Bezout identity.

How the result is produced

1

Repeated division

Divide the larger input by the smaller and record the quotient and remainder. Replace the pair with (smaller, remainder) and repeat until the remainder is 0; the last nonzero remainder is the GCD. Each quotient counts how many squares of that step's size fit across the current rectangle, so the animation mirrors the arithmetic. Termination is fast: the numbers shrink by about half every two steps.

2

Bezout coefficients

The extended pass threads two auxiliary coefficient pairs through the same division steps so the final line reads ax + by = gcd(a,b). These coefficients are not unique: with g = gcd(a,b), adding (b/g, -a/g) to (x, y) yields another valid pair, so the tool shows one representative, usually the smallest in absolute value. It underlies modular inverses and the integer solutions of ax + by = c.

Good uses

  • Reducing a fraction: divide numerator and denominator by their GCD to get the simplest form. For 252/105 the GCD is 21, so the fraction reduces to 12/5.
  • Finding an LCM without prime factorization: lcm(a,b) = a x b / gcd(a,b). That is what you need to add fractions with different denominators or to find when two repeating schedules first coincide.
  • Getting a modular inverse: when gcd(a,m) = 1, the Bezout identity ax + my = 1 shows x is an inverse of a modulo m, which is what division means in modular arithmetic, as used in RSA-style calculations.

Limits and checks

  • Order and signs do not matter: gcd(105, 252) equals gcd(252, 105), and the GCD is taken over absolute values, so one negative input gives the same result as its positive counterpart. If the tool rejects negatives or demands a specific order, that is a UI restriction, not part of the mathematics.
  • Zero inputs are the degenerate case: gcd(0, a) = a for any a, and gcd(0, 0) = 0 by convention. A zero input means no division actually happens, so the step list is empty or trivial and the rectangle animation collapses to a strip. Very large inputs also make the geometric view impractical even though the arithmetic is instant.
  • The displayed Bezout pair is one answer among many: if ax + by = g, then (x + b/g, y - a/g) is another valid pair, so the output is a representative, not the pair. And a general equation ax + by = c has integer solutions if and only if g divides c; the Bezout output alone does not settle that.

Common questions

Why does the rectangle view break the inputs into squares?

The GCD is the side length of the largest square that tiles an a-by-b rectangle exactly. Each step of the algorithm cuts the largest possible square out of the current region, and the square size that finally leaves no leftover is the GCD. The counts per step are exactly the division quotients, so the picture and the arithmetic tell the same story.

Can I use this to find a modular inverse, like 1/a mod m?

Not as a dedicated operation, but the Bezout output is exactly what you need. When gcd(a,m) = 1 the identity reads ax + my = 1, so x reduced modulo m is a's inverse; when gcd(a,m) > 1 no inverse exists and no coefficient pair changes that. Reduce x into the range 0 to m-1 yourself.

References and verification

The example and behavioral notes were checked against the browser implementation. Standards and primary references below define the relevant format, formula, or platform behavior.

Related Tools