Euclid’s Algorithm: GCD By Repeated Remainders

Find a gcd fast without factoring by repeatedly taking remainders until the remainder hits 00. You will see why the numbers shrink so quickly, how to run the steps reliably, and how the same work can also produce ax+by=gcd(a,b)ax+by=\gcd(a,b).

Factoring feels like the natural way to find a greatest common divisor, until the numbers get big and the whole plan collapses. Euclid’s algorithm sidesteps factoring completely. It uses one move, take a remainder, and each move makes the numbers smaller. The surprise is that remainders are not a hack. They preserve the information a gcd cares about while discarding everything else.

The shortcut is remainders

The gcd of two numbers cares about what divides them both. When you replace a big number with its remainder after dividing by the smaller number, you keep the set of common divisors the same, but you usually shrink the problem dramatically.

To make the remainder idea feel concrete, it helps to see it as repeated subtraction. Dividing 360360 by 8484 leaves the same remainder as subtracting 8484 over and over until you cannot anymore. That leftover is the part that still might share a divisor with 8484.

Try the remainder picture below and notice how quickly the leftover drops compared to the original number.

Rule of thumb
If you can replace a number by something smaller without changing which numbers divide it, you are getting closer to the gcd.

What greatest common divisor really means

A divisor of aa is a number that goes into aa with no remainder. You will also see this written as dad\mid a, read as dd divides aa. A common divisor of aa and bb divides both. The greatest common divisor (gcd) is the largest positive common divisor.

Two edge cases are worth locking in early because they keep the algorithm from feeling like magic later.

  • If a=ba=b, then gcd(a,b)=a\gcd(a,b)=a.
  • If b=0b=0, then gcd(a,0)=a\gcd(a,0)=a.

That second one is not a special trick. Every number divides 00, since 0=a00=a\cdot 0. So the common divisors of aa and 00 are exactly the divisors of aa, and the greatest of those is aa.

If you want quick definitions and edge case reminders as you read, open the mini-cards here.

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

Euclid’s algorithm rests on one identity.

Explanation: If a=bq+ra=bq+r with 0r<b0\le r<b, then gcd(a,b)=gcd(b,r)\gcd(a,b)=\gcd(b,r). Any number that divides both aa and bb also divides r=abqr=a-bq. Any number that divides both bb and rr also divides a=bq+ra=bq+r. So the sets of common divisors match.

The line a=bq+ra=bq+r is the division algorithm. It says when you divide aa by bb, you get a quotient qq and a remainder rr. The remainder rr is exactly amodba\bmod b.

The key is the subtraction idea hiding inside it. Since r=abqr=a-bq, you get rr by taking aa and subtracting a whole-number multiple of bb. Subtracting a multiple of bb cannot change whether something divides both numbers.

Use the annotated equation below to track which pieces matter for divisibility.

Once you accept this identity, the algorithm is just applying it repeatedly until the remainder becomes 00.

Running Euclid’s algorithm by hand

Euclid’s algorithm is a loop with a clean stopping rule.

Start with aa as the larger number and bb as the smaller positive number.

  • Divide aa by bb to get a remainder rr.
  • Replace (a,b)(a,b) with (b,r)(b,r).
  • Stop when r=0r=0. The gcd is the last nonzero remainder, which is the current bb.

A table keeps you from losing track, especially when the quotients change from step to step. Each row is one division a=bq+ra=bq+r. The moment you see r=0r=0, you do not need any more work.

Fill in the iteration table here, and watch how the last nonzero remainder gets singled out.

The quotients qq are not the goal. They are just bookkeeping that lets you produce the remainder reliably.

Stopping rule
When the remainder hits 00, the current divisor is the gcd. Not the 00, not the previous dividend.

Why it always stops

Each remainder is smaller than the number you divided by. If you start with b>0b>0, the remainders form a strictly decreasing sequence of nonnegative integers:

b>r1>r2>0.b>r_1>r_2>\cdots\ge 0.

A strictly decreasing list of nonnegative integers cannot go on forever, so eventually a remainder becomes 00 and the algorithm terminates.

How many steps should you expect? Usually not many.

The numbers tend to shrink fast because remainders are bounded by what you are dividing by. The slowest case happens when the remainders shrink as little as possible each time. That pattern is tied to consecutive Fibonacci numbers, and even then the step count grows only logarithmically with the size of the inputs. For beginner intuition, read that as doubling the size of the inputs adds only a small number of extra remainder steps.

Extended Euclid preview

Plain Euclid gives you d=gcd(a,b)d=\gcd(a,b). Extended Euclid gives more. It finds integers xx and yy such that

ax+by=d.ax+by=d.

This looks mysterious until you notice it is just undoing the remainder steps. Each remainder was built from the previous two numbers using a subtraction of a multiple. Working backward turns the final gcd into a combination of the original inputs.

Why care about ax+by=dax+by=d.

  • It proves the gcd really is the smallest positive number you can build from aa and bb using integer combinations.
  • When d=1d=1, it gives a way to find a modular inverse, the number that multiplies by aa to make 11 modulo bb.

Compare plain output versus extended output here so you can see what extra information appears.

You do not need extended Euclid to compute a gcd, but it is the same story with one extra layer of bookkeeping.

Where this shows up

Reducing fractions is the everyday use. To simplify ab\frac{a}{b}, compute d=gcd(a,b)d=\gcd(a,b) and divide top and bottom by dd. Euclid’s algorithm makes that step fast even when aa and bb are large.

Modular arithmetic uses gcd as a gatekeeper. If gcd(a,m)=1\gcd(a,m)=1, then aa has a multiplicative inverse modulo mm. If the gcd is larger than 11, it does not. That single gcd check prevents a lot of confusion.

When you want to fact-check your gcd result without refactoring everything, try these quick tests.

  • Your gcd must divide both original numbers.
  • Your gcd cannot be larger than the smaller input.
  • If you get a remainder wrong, later remainders often look too large. They should generally trend downward fast.

If you want a concrete next step, pick two numbers you care about from your own work, a fraction, a timestamp pair, anything, and run Euclid’s algorithm until you can do the remainder steps without hesitation. Speed comes from confidence in the stopping rule, not from mental arithmetic tricks.

Was this lesson helpful?
Dive Deeper

Generate a follow-up sub-lesson on any aspect of this topic

Related content