Euclid’s Algorithm: GCD By Repeated Remainders
Find a gcd fast without factoring by repeatedly taking remainders until the remainder hits . You will see why the numbers shrink so quickly, how to run the steps reliably, and how the same work can also produce .
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 by leaves the same remainder as subtracting over and over until you cannot anymore. That leftover is the part that still might share a divisor with .
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 is a number that goes into with no remainder. You will also see this written as , read as divides . A common divisor of and 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 , then .
- If , then .
That second one is not a special trick. Every number divides , since . So the common divisors of and are exactly the divisors of , and the greatest of those is .
If you want quick definitions and edge case reminders as you read, open the mini-cards here.
Why
Euclid’s algorithm rests on one identity.
Explanation: If with , then . Any number that divides both and also divides . Any number that divides both and also divides . So the sets of common divisors match.
The line is the division algorithm. It says when you divide by , you get a quotient and a remainder . The remainder is exactly .
The key is the subtraction idea hiding inside it. Since , you get by taking and subtracting a whole-number multiple of . Subtracting a multiple of 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 .
Running Euclid’s algorithm by hand
Euclid’s algorithm is a loop with a clean stopping rule.
Start with as the larger number and as the smaller positive number.
- Divide by to get a remainder .
- Replace with .
- Stop when . The gcd is the last nonzero remainder, which is the current .
A table keeps you from losing track, especially when the quotients change from step to step. Each row is one division . The moment you see , 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 are not the goal. They are just bookkeeping that lets you produce the remainder reliably.
Stopping rule
When the remainder hits , the current divisor is the gcd. Not the , not the previous dividend.
Why it always stops
Each remainder is smaller than the number you divided by. If you start with , the remainders form a strictly decreasing sequence of nonnegative integers:
A strictly decreasing list of nonnegative integers cannot go on forever, so eventually a remainder becomes 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 . Extended Euclid gives more. It finds integers and such that
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 .
- It proves the gcd really is the smallest positive number you can build from and using integer combinations.
- When , it gives a way to find a modular inverse, the number that multiplies by to make modulo .
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 , compute and divide top and bottom by . Euclid’s algorithm makes that step fast even when and are large.
Modular arithmetic uses gcd as a gatekeeper. If , then has a multiplicative inverse modulo . If the gcd is larger than , 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.
Generate a follow-up sub-lesson on any aspect of this topic