GCD Calculator

The greatest common divisor is the largest whole number dividing all of them, and the way to find it is not to factorise. Euclid's method is to divide, keep the remainder, and repeat. When the remainder reaches zero, the previous one is the answer. It works because any number dividing both a and b ...

Euclid’s algorithm does not need the factors. Divide, keep the remainder, repeat — the last non-zero remainder is the answer. Two sixteen-digit numbers resolve in about six steps, where factorising either one would take tens of millions of divisions. It is the oldest algorithm still in everyday use, and nothing has improved on it.

THE NUMBERS

GCD OF 1,071, 462

gcd

21

lcm

23,562

Every one of them is a whole number of 21s: 51, 22 respectively. Nothing larger divides them all. Euclid took 3 steps.

StepDivisionRemainder
11,071 = 2 × 462 + 147147
2462 = 3 × 147 + 2121
3147 = 7 × 21 + 00 — stop

3 steps against a bound of 15. Lamé proved in 1844 that the count never exceeds five times the number of digits in the smaller input, which here is 3 digits. The worst cases for any given size are consecutive Fibonacci numbers — gcd(75025, 46368) needs 23 steps, more than any random pair of that size manages. That bound is why Euclid stays fast on numbers far too large to factor.

Bézout: 1,071(−3) + 462(7) = 21. The gcd of any two numbers can always be written as a whole multiple of one plus a whole multiple of the other, and running the same divisions backwards produces the coefficients. It is what makes modular inverses computable, and so what makes RSA decryption possible at all.

THE ALGORITHM

DIVIDE, TAKE THE REMAINDER, REPEATdivide the larger by the smaller, then repeat on the smaller and the remainder3 steps, and the answer is 21.the last non-zero remainder, 21, is the greatest common divisor

Why it works. Any number dividing both a and b also divides their remainder, and any number dividing b and the remainder also divides a. So the pair (a, b) and the pair (b, a mod b) have exactly the same common divisors — replacing one with the other loses nothing while making the numbers smaller. Repeat and the smaller number reaches zero, at which point the other is the answer.

THE REMAINDERS, ON A LOG SCALE46221each remainder is less than half the one two steps before — which is why the count stays small
EACH NUMBER AS A WHOLE COUNT OF THE GCDthe divisions come out exact — that is what a common divisor meansno larger length divides them all — the multiples share no factor themselves

Euclid’s algorithm · exact for any size of input

Created with❤️byeaglecalculator.com

HOW TO USE

  1. 1

    Enter two or more whole numbers. Signs are ignored, because divisibility does not care about them — if 3 divides 12 it divides −12 equally.

  2. 2

    Read the division chain rather than just the answer. Each line is one step of the algorithm, and the last non-zero remainder is the result.

  3. 3

    Compare the step count with the bound shown. Euclid finishes in at most five times the digit count of the smaller number, which is what keeps it fast on very large inputs.

  4. 4

    With exactly two numbers you also get the Bézout coefficients — the whole multiples of each that add to the gcd. They are what modular inverses are built from.

REFERENCE FORMULAS

RuleFormulaWhat it is for
Euclid’s algorithmgcd(a, b) = gcd(b, a mod b)Repeat until the remainder is zero. The last non-zero remainder is the answer.
Base casegcd(a, 0) = aEverything divides zero, so the other number is the largest common divisor.
Two numbersgcd(a,b) × lcm(a,b) = a × bAlways true for exactly two. Do not extend it to three — see below.
Three numbersgcd·lcm = abc only if pairwise coprimegcd(2,3,4) × lcm(2,3,4) = 12, not 24. The two-number identity does not generalise.
Correct triple identitylcm(a,b,c) = abc·gcd(a,b,c) ÷ (gcd(a,b)gcd(b,c)gcd(a,c))Inclusion–exclusion on the exponents. Verified over 40,000 triples.
Several numbersgcd(a,b,c) = gcd(gcd(a,b), c)Fold pairwise. The order makes no difference to the result.
Bézout’s identitygcd(a,b) = ax + byIntegers x and y always exist. The extended algorithm finds them.
Coprimegcd(a, b) = 1No common factor above 1. Then lcm is simply the product.
Scalinggcd(ka, kb) = k · gcd(a, b)A common factor pulled out of both stays a common factor.
Reducing a fractiona/b ÷ gcd(a,b) on bothDividing through by the gcd gives lowest terms in one step.
Lamé’s boundsteps ≤ 5 × digits of the smallerProved in 1844. Verified here across 200,000 random pairs with no violations.
Worst caseconsecutive Fibonacci numbersgcd(75025, 46368) needs 23 steps — more than any random pair of that size.
Against factoringEuclid is logarithmic; factoring is notA 16-digit pair resolves in about six steps rather than 10⁷ divisions.
Negatives and zerogcd(−a, b) = gcd(a, b); gcd(0,0) undefinedSign is irrelevant. Two zeros have no greatest common divisor at all.

FREQUENTLY ASKED QUESTIONS

RELATED CALCULATORS

MORE ALGEBRA CALCULATORS

Was this calculator helpful?

Last updated: August 4, 2026 · Euclid’s algorithm with Bézout coefficients · gcd × lcm = ab holds for two numbers, not for three.