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.
252 = 105 · 2 + 42105 = 42 · 2 + 2142 = 21 · 2 + 0stopgcd = 21
252 and 105, frozen

The launch chain: remainders 42, then 21, then 0 — three quotients of 2 in a row, and the purple box closes on 21.

The frozen frame above is the tool exactly as it opens: 252=1052+42252 = 105 \cdot 2 + 42, then 105=422+21105 = 42 \cdot 2 + 21, then 42=212+042 = 21 \cdot 2 + 0. Three rows, three identical quotients of 2, and the purple box closing on 21.

The launch example is well chosen: big enough that listing divisors would be tedious (252 has eighteen of them), small enough that each division is mental arithmetic. When you want other behaviors — instant finishes, coprime grinds, worst cases — the presets supply them one click away.

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: the launch pair (252,105)(252, 105) giving GCD 2121, the quick finish (462,198)(462, 198) giving 6666, the textbook example (1071,462)(1071, 462) giving 2121, the swap-then-finish case (56,84)(56, 84) ending at 2828, and the coprime pair (35,54)(35, 54) with GCD 11.

The presets are deliberately chosen to demonstrate the algorithm's qualitative behaviors: coprime cases that grind through many steps, quick finishes that end in two rows, and mid-sized examples that produce a satisfyingly visual chain of about 3–5 rows. Two more states are worth typing by hand: a Fibonacci pair like 8989 and 144144 for the algorithm's worst case, and equal numbers for its shortest run.

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.
36 = 36 · 1 + 0stopgcd = 36
36 and 36, frozen

One row: 36 = 36 · 1 + 0. The remainder is zero on arrival, and gcd(36, 36) = 36 — the shortest chain the tool can draw.

The frozen frame above is the base case made visible: with equal inputs the very first division reads 36=361+036 = 36 \cdot 1 + 0, the remainder is zero on arrival, and gcd(36,36)=36\gcd(36, 36) = 36 ends the story in one row — the shortest chain the tool can draw.

Termination and correctness meet in that final line. Every chain, however long, is marching toward some pair (d,0)(d, 0); the special cases differ only in how many substitutions the journey takes.

Short Chains: When a Remainder Divides Its Divisor

Try the preset (462,198)(462, 198). The first division gives 462=1982+66462 = 198 \cdot 2 + 66, and then something decisive happens: 6666 divides 198198 exactly, so the second row reads 198=663+0198 = 66 \cdot 3 + 0 and the algorithm stops at GCD 6666.

Every chain ends this way — the question is only how soon. The algorithm terminates precisely when a remainder divides the number it is about to be paired with. In short chains that happens on the first try; in long ones the pair has to shrink many times first.

A useful way to watch the tool: after each row, ask "does this remainder divide the number above it?" The moment the answer is yes, the next row is the last.
462 = 198 · 2 + 66198 = 66 · 3 + 0stopgcd = 66
462 and 198, frozen

Remainder 66 drops down, divides 198 exactly, and the chain stops at two rows: gcd = 66.

The two-row pattern has a clean characterization: the chain for (a,b)(a, b) has exactly two rows when the first remainder amodba \bmod b divides bb without being zero. The preset (56,84)(56, 84) under special cases is the same story with a swap in front.

At the other extreme sit pairs whose remainders keep refusing to divide — the coprime pairs that grind down to 1, and the Fibonacci pairs that do so as slowly as arithmetic allows.

The Textbook Example: 1071 and 462

The pair (1071,462)(1071, 462) is the classic worked example of the Euclidean algorithm — it has appeared in textbooks for over a century. The chain runs:

1071=4622+1471071 = 462 \cdot 2 + 147


462=1473+21462 = 147 \cdot 3 + 21


147=217+0147 = 21 \cdot 7 + 0


Three rows, GCD 2121. The example earns its fame by exercising the algorithm's range: quotients of 22, 33, and then 77, remainders that fall fast (147147, 2121, 00), and four-digit input dispatched in three lines of arithmetic.

Notice the coincidence with the launch pair: gcd(252,105)=21\gcd(252, 105) = 21 as well. Different pairs, same answer — infinitely many pairs share any given GCD.
1071 = 462 · 2 + 147462 = 147 · 3 + 21147 = 21 · 7 + 0stopgcd = 21
1071 and 462, frozen

The century-old textbook chain: three rows ending in a quotient of 7, and the same answer as the launch pair — 21.

The chain also demonstrates how little the algorithm cares about size. Listing the divisors of 1071=327171071 = 3^2 \cdot 7 \cdot 17 by hand would be real work; three divisions need none of it. That contrast — divisions instead of factorizations — is the practical content of why the algorithm works.

For input this size the chain is still short. To see the algorithm genuinely struggle, hand it a Fibonacci pair and watch every quotient collapse to 1.

Coprime Pairs: When the GCD Is 1

Try the preset (35,54)(35, 54). After the automatic swap, the chain runs through five divisions with remainders 1919, 1616, 33, 11, and finally 00: the GCD is 11.

Two integers with GCD 11 are called coprime (or relatively prime). They need not be prime themselves — 35=5735 = 5 \cdot 7 and 54=23354 = 2 \cdot 3^3 are both composite — they simply share no prime factor.

Coprime pairs make the algorithm work hardest for its answer: with no common factor to find, the chain must grind all the way down to 11 before a remainder of 00 can appear. Relative to their size, coprime pairs produce the longest chains.
54 = 35 · 1 + 1935 = 19 · 1 + 1619 = 16 · 1 + 316 = 3 · 5 + 13 = 1 · 3 + 0stopgcd = 1
54 and 35, frozen

Five divisions, remainders 19, 16, 3, 1, 0: nothing divides both 54 and 35 except 1 — a coprime pair.

Coprimality is the property that matters most in practice: a fraction a/ba/b is fully reduced exactly when aa and bb are coprime, and a number is invertible modulo nn exactly when it is coprime to nn — the fact underlying the modular arithmetic applications of the GCD.

Coprime pairs are also abundant: two random integers are coprime with probability 6/π261%6/\pi^2 \approx 61\%, a celebrated result that connects the GCD to π\pi. The extreme members of the family are the Fibonacci neighbors, which are always coprime and always slowest.

The Worst Case: Fibonacci Pairs

Enter 8989 and 144144 — two consecutive Fibonacci numbers. The chain takes ten rows, and every quotient except the final one is 11: each division removes as little as possible, so the pair shrinks at the slowest rate the algorithm allows.

The reason is the Fibonacci recurrence itself. Since Fn+1=Fn+Fn1F_{n+1} = F_n + F_{n-1} with Fn1<FnF_{n-1} < F_n, dividing Fn+1F_{n+1} by FnF_n always gives quotient 11 and remainder Fn1F_{n-1} — so the algorithm walks the Fibonacci sequence backwards, one term per row, all the way down to gcd=1\gcd = 1.

This is not just a curiosity but the exact worst case: a theorem of Gabriel Lamé (1844) states that if the algorithm takes nn steps, the smaller input is at least the Fibonacci number Fn+1F_{n+1}. No inputs of a given size can be slower than Fibonacci neighbors.
144 = 89 · 1 + 5589 = 55 · 1 + 3455 = 34 · 1 + 2134 = 21 · 1 + 1321 = 13 · 1 + 813 = 8 · 1 + 58 = 5 · 1 + 35 = 3 · 1 + 23 = 2 · 1 + 12 = 1 · 2 + 0stopgcd = 1
144 and 89, frozen

Ten rows for two three-digit numbers: with every quotient 1 until the last, Fibonacci neighbors shrink as slowly as the algorithm allows.

Lamé's bound is why the algorithm is fast: the step count grows only logarithmically — roughly five steps per digit of the smaller number, even in the worst case. His 1844 argument is often called the first complexity analysis of an algorithm, a century before computers existed to run one.

Fibonacci neighbors are also always coprime — the backwards walk always ends at 11 — making them the extreme members of the coprime pairs family.

Special Cases and Corner Behavior

A few input patterns produce notably short or long chains:

A swap, then a quick finish — try the preset (56,84)(56, 84). Since 56<8456 < 84 the pair is swapped first; the division gives 84=561+2884 = 56 \cdot 1 + 28, and 2828 divides 5656, so 56=282+056 = 28 \cdot 2 + 0 stops the algorithm at GCD 2828 after just two steps. (When the smaller number actually divides the larger — try (28,84)(28, 84) — the chain ends in a single row.)

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 — the base case gcd(d,0)=d\gcd(d, 0) = d in action, as explained under why the algorithm works.

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.
84 = 56 · 1 + 2856 = 28 · 2 + 0stopgcd = 28
84 and 56, frozen

The inputs arrive as (56, 84) and are swapped; remainder 28 then divides 56 exactly, ending the chain at gcd = 28.

The frozen frame above shows the (56,84)(56, 84) case: a swap, one substantial division, one clean finish. Between such quick runs and the ten-row Fibonacci grind lies the algorithm's whole spectrum of behavior, and the Random pair button samples it — most random pairs land in the middle, at three to five rows.

The swap itself is worth a note: the algorithm never needs to be told which number is larger. If a<ba < b, the first division simply produces quotient 00 and remainder aa, which performs the swap automatically — the tool just does it up front to keep the chain tidy.