Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools


Euclidean Algorithm


Euclidean Algorithm Visualizer

Type any two whole numbers, or try a preset. Each line below divides and keeps the leftover — the pair shrinks until nothing is left over, and the last divisor standing is the greatest common divisor.

21
gcd(252, 105) = 21The largest number that divides both 252 and 105 evenly is 21 (found in 3 steps).
Euclidean algorithm chain for gcd(252, 105)Vertical chain of division steps reducing gcd(252, 105) to 21.252 = 105 · 2 + 42Remainder 42 becomes the divisor on the next line.105 = 42 · 2 + 21Remainder 21 becomes the divisor on the next line.Greatest common divisor = 21 (the last nonzero remainder).42 = 21 · 2 + 0stopgcd = 21
remainder (the leftover) becomes the next divisor greatest common divisor
How the Euclidean algorithm works

The idea is older than algebra: the greatest common divisor of two numbers does not change if you replace the larger number with the remainder after dividing it by the smaller one.

  1. Divide the bigger number by the smaller one and keep the remainder.
  2. Replace the pair: the old divisor becomes the new dividend, the remainder becomes the new divisor.
  3. Repeat. Each remainder is smaller than the last, so the process always ends.
  4. When the remainder reaches 0, the divisor on that final line is the answer.

Hover any amber remainder above to see it drop down and become the divisor on the next line — that single move is the whole algorithm.








Getting Started with the Visualizer

Open the page and a friendly banner appears at the top once you have two numbers entered. By default it launches with a=252a = 252 and b=105b = 105 — a classic textbook example where the greatest common divisor is 2121.

The visualizer has four main parts arranged top to bottom:

Input controls — two number fields for aa and bb, plus a Random pair button and five preset pairs
Result banner — large purple number showing the GCD, with a plain-language sentence explaining what it means
Division chain — a vertical stack of equations of the form a=bq+ra = b \cdot q + r, each step shrinking the pair, with amber remainder pills and dashed arrows connecting each remainder to the next divisor
Steps list and legend — a side panel listing every division as plain text, plus a color legend

A collapsible "How the Euclidean algorithm works" panel sits at the bottom for the underlying mathematical idea.

Entering Numbers and Using Presets

Type any two positive whole numbers into the aa and bb fields. The visualizer recomputes the entire chain instantly on every keystroke. If a<ba < b, the algorithm internally swaps them — the larger of the two is always the first dividend.

For exploration without typing, three quick options live next to the input fields:

Random pair — generates two random integers between 12 and 480 and runs the algorithm on them. Useful for sampling the variety of chain lengths that arise from different inputs.
Five preset pairs — curated examples spanning easy and interesting cases: (252,105)(252, 105) giving GCD 2121, (462,198)(462, 198) giving 6666, (1071,462)(1071, 462) giving 2121, (56,84)(56, 84) where one input divides the other, and (35,54)(35, 54) which are coprime (GCD 11).

The presets are deliberately chosen to demonstrate the algorithm's qualitative behaviors: coprime cases that grind through many steps, divisor cases that finish in one step, and mid-sized examples that produce a satisfyingly visual chain of about 3–5 rows.

Reading a Division Row

Each row of the chain shows one application of the division algorithm:

dividend=divisorquotient+remainder\text{dividend} = \text{divisor} \cdot \text{quotient} + \text{remainder}


Read left to right: the dividend is the larger of the current pair, the divisor is the smaller, the quotient is how many whole times the divisor fits into the dividend, and the remainder is what is left over.

The remainder is highlighted in an amber pill to draw the eye — it is the key piece that becomes the divisor of the *next* row. The final divisor, when the remainder finally hits zero, is shown in a purple box with a "gcd = N" callout below it. The terminating zero remainder appears as a dashed gray pill with an italic "stop" label, indicating that the algorithm has finished.

Every numerical field is rendered in monospace so the rows align vertically. The visual chain reads top to bottom as a step-by-step computation that the eye can follow without effort.

Following the Substitution Arrows

Between every pair of consecutive rows, a dashed amber Bezier arrow sweeps from the remainder pill of one row down to the divisor position of the next. This is the visual representation of the core algorithmic substitution:

new pair=(old divisor,old remainder)\text{new pair} = (\text{old divisor}, \text{old remainder})


The arrow makes the substitution concrete. You can literally see the number that was a remainder in row ii become the divisor in row i+1i + 1, repeating until a remainder of zero finally appears. The whole algorithm is captured by that single recurring move.

The arrows are deliberately subtle by default — drawn in the same amber as the remainder pills — so they read as decoration when you scan but become useful when you study a particular step. Hovering a remainder darkens its arrow and turns it purple, making the connection unmistakable.

Hovering Remainders and Steps

Two coordinated hover interactions help you trace the algorithm:

Hover an amber remainder pill — its arrow lights up in purple, and the divisor on the next row gets a purple ring around it. Both endpoints of the substitution glow together, letting you confirm which remainder becomes which divisor.
Hover an entry in the side Steps list — the same row in the diagram lights up. Useful when you want to inspect a specific step from the textual summary without scrolling.

Both hover targets connect the textual representation (numbered list of equations on the right) to the graphical chain (the diagram in the center). Wherever your attention lands, the corresponding parts in the other view highlight automatically.

The hover state is also exposed as a native browser tooltip on the SVG elements, so screen readers and accessibility tools can convey the substitution relationship in plain text.

The Result Banner and GCD Callout

Above the diagram, a purple banner shows the result in three forms simultaneously:

• A large standalone number — the GCD itself, in big bold purple
• A monospace equationgcd(a,b)=N\gcd(a, b) = N in the canonical mathematical form
• A plain-language sentence — "The largest number that divides both aa and bb evenly is NN (found in kk steps)"

The triple presentation is deliberate. The standalone number is for someone who just wants the answer. The equation is for someone copying the result into a homework problem. The sentence is for a beginner first encountering the concept and needing to know what "greatest common divisor" actually means.

Below the diagram, the final divisor — the GCD — appears in a purple-bordered box with a callout line and the label "gcd = N" beneath it. This visual punctuation marks the chain's endpoint and connects the diagram back to the banner.

What Is the Greatest Common Divisor?

The greatest common divisor (GCD) of two integers is the largest integer that divides both of them with no remainder. Equivalently, it is the largest member of their common set of divisors.

For small numbers, you could find the GCD by listing every divisor of each number and picking the biggest one they share. For example:

• Divisors of 1212: 1,2,3,4,6,121, 2, 3, 4, 6, 12
• Divisors of 1818: 1,2,3,6,9,181, 2, 3, 6, 9, 18
• Common divisors: 1,2,3,61, 2, 3, 6
• Greatest common divisor: gcd(12,18)=6\gcd(12, 18) = 6

This naive approach works but becomes slow for large numbers. The Euclidean algorithm — the method visualized here — finds the GCD without ever listing divisors. For gcd(252,105)\gcd(252, 105) it takes only three division steps, regardless of how many divisors 252252 and 105105 actually have.

The GCD is foundational for simplifying fractions, modular arithmetic, and number theory. Two numbers with GCD equal to 11 are called coprime — they share no common factors and are in some sense "as different as possible" multiplicatively.

Why the Algorithm Works

The Euclidean algorithm rests on a single mathematical fact, older than algebra itself:

gcd(a,b)=gcd(b,amodb)\gcd(a, b) = \gcd(b, a \bmod b)


In words: the greatest common divisor of two numbers stays the same if you replace the larger number with its remainder after division by the smaller. This is the invariant that the algorithm preserves at every step.

The argument is short. Any common divisor of aa and bb also divides aqb=ra - q \cdot b = r, so it is a common divisor of bb and rr as well. Going the other way, any common divisor of bb and rr divides bq+r=ab \cdot q + r = a, so it is a common divisor of aa and bb. The two pairs have *exactly the same* set of common divisors, hence the same greatest common divisor.

The algorithm terminates because every step strictly decreases the smaller of the two numbers. A strictly decreasing sequence of non-negative integers must eventually reach zero. When it does, the partner — the last nonzero remainder — is the GCD, because gcd(d,0)=d\gcd(d, 0) = d.

Special Cases and Corner Behavior

A few input patterns produce notably short or long chains:

One number divides the other — try the preset (56,84)(56, 84). The first division gives 84=561+2884 = 56 \cdot 1 + 28, then 56=282+056 = 28 \cdot 2 + 0, and the algorithm stops at GCD 2828 after just two steps. Whenever the smaller number is a divisor of the larger, the chain terminates in one or two rows.

Coprime numbers — try the preset (35,54)(35, 54). The GCD is 11, and the chain works through several reductions before finally hitting a remainder of 11 and then 00. Coprime pairs tend to produce the *longest* chains relative to the size of the numbers.

Consecutive Fibonacci numbers — try entering, e.g., 8989 and 144144. These pairs produce the absolute worst-case behavior of the algorithm and the longest chains for their magnitude. The reason is built into the recursive definition of Fibonacci numbers themselves.

Equal numbers — entering the same number twice gives a one-step chain ending immediately at the GCD, which is that number.

Running the algorithm on a few of these intentionally extreme cases gives a feel for how the input size relates to the chain length — and why the Euclidean algorithm is considered remarkably efficient even on enormous inputs.

Related Concepts and Tools

Greatest Common Divisor — the mathematical concept the visualizer computes; the theoretical companion page to this tool.

Least Common Multiple (LCM) — the dual concept, related to the GCD by lcm(a,b)=ab/gcd(a,b)\text{lcm}(a, b) = a \cdot b / \gcd(a, b).

Division Algorithm — the basic theorem that for any positive integers aa and bb, there exist unique qq and rr with 0r<b0 \leq r < b such that a=bq+ra = b \cdot q + r. Every row of the visualizer is one application of this.

Coprime Integers — pairs with GCD equal to 11; foundational for modular arithmetic and number theory.

Prime Factorization — an alternative route to the GCD via factoring both numbers and multiplying their shared prime powers. Slower for large numbers than the Euclidean algorithm but more transparent for small ones.

Bezout's Identity — the extended Euclidean algorithm finds integers xx and yy such that ax+by=gcd(a,b)ax + by = \gcd(a, b). A natural follow-up tool to this one.

Modular Arithmetic — the GCD underlies which numbers are invertible modulo nn and is the foundation of many cryptographic schemes.

Fibonacci Numbers — the worst-case inputs for the Euclidean algorithm, related to it through deep recursive structure.