Formula Bro

Math and physics formulas, explained like you are new.


Euclidean Algorithm

In one sentence: The Euclidean algorithm finds the largest number that divides two numbers evenly, by replacing the pair with a smaller pair again and again until the remainder is zero.

gcd⁡(a,b)=gcd⁡(b,amod⁡b),gcd⁡(a,0)=a\gcd(a, b) = \gcd(b,\ a \bmod b), \qquad \gcd(a, 0) = a

Read it out loud: “The greatest common divisor of a and b equals the greatest common divisor of b and a mod b. Here a mod b is the remainder when a is divided by b. The greatest common divisor of a and zero is a. The symbol gcd stands for greatest common divisor.”

Before you start

The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.

Divides evenly

A number dd divides another number aa evenly when splitting aa into groups of dd leaves nothing over. For example, 3 divides 12 evenly, because 12 is four groups of 3 with nothing left. A number that divides aa this way is called a divisor of aa. The number 1 divides every whole number.

Common divisor and the greatest common divisor, gcd\gcd

A common divisor of two numbers divides both of them evenly. The greatest common divisor, written gcd⁡(a,b)\gcd(a, b), is the largest of these. For 12 and 18 the common divisors are 1, 2, 3 and 6, so gcd⁡(12,18)=6\gcd(12, 18) = 6. The name gcd stands for greatest common divisor.

Remainder and amod⁡ba \bmod b

The remainder is what is left after dividing as many full groups as possible. It is written amod⁡ba \bmod b, and it is always smaller than bb. For example, 48mod⁡18=1248 \bmod 18 = 12, because 48 holds two full groups of 18, which make 36, with 12 left over.

Multiple and least common multiple

A multiple of a number is that number times a whole number, such as 6, 12, 18 and so on for the number 6. The least common multiple of two numbers is the smallest number that is a multiple of both. It is a different idea from the greatest common divisor, because it finds the smallest shared multiple, not the largest shared divisor.

What each symbol means

SymbolMeaning

gcd⁡(a,b)\gcd(a, b)

The greatest common divisor: the largest whole number that divides both aa and bb with no remainder. It has no unit, because it counts a number of parts rather than a measured amount. For 48 and 18 it is 6.

aa

A whole number, 0 or more, and the first number of the pair. It has no unit in the algorithm itself. If you are cutting strips, aa is the length of the first strip, and the GCD has the same length unit.

bb

A whole number, 0 or more, and the second number of the pair. The rule replaces the pair (a,b)(a, b) with the smaller pair (b,amod⁡b)(b, a \bmod b), so bb becomes the new first number at each step.

amod⁡ba \bmod b

The remainder when aa is divided by bb: the leftover after taking as many full groups of bb as possible from aa. It is always less than bb, which is why the numbers keep shrinking at every step of the algorithm.

The formula piece by piece

amod⁡ba \bmod b

The remainder when aa is divided by bb. It is always smaller than bb. In the example, 48mod⁡18=1248 \bmod 18 = 12, and 12 is smaller than 18, so the numbers shrink.

(b,amod⁡b)(b,\ a \bmod b)

The new pair. The old second number bb becomes the new first number, and the remainder becomes the new second number. In the example, the first step turns the pair (48, 18) into the new pair (18, 12).

gcd⁡(a,b)=gcd⁡(b,amod⁡b)\gcd(a, b) = \gcd(b,\ a \bmod b)

The key step in one line: the greatest common divisor of the old pair equals the greatest common divisor of the new pair. Nothing is lost, because the two pairs share exactly the same common divisors, so their largest one is the same.

gcd⁡(a,0)=a\gcd(a, 0) = a

The stopping rule. When the second number is 0, the first number is the answer. Every number divides 0 evenly, so the largest common divisor of aa and 0 is aa itself.

gcd⁡(a,b)=gcd⁡(b,amod⁡b),gcd⁡(a,0)=a\gcd(a, b) = \gcd(b,\ a \bmod b), \qquad \gcd(a, 0) = a

The whole formula in one sentence: the greatest common divisor of aa and bb equals the greatest common divisor of bb and the remainder amod⁡ba \bmod b, and the greatest common divisor of aa and zero is aa.

What the formula is doing, and why

The greatest common divisor, or GCD, of two whole numbers is the largest number that divides both of them with no remainder. Listing every divisor of two large numbers and comparing the lists is slow. The Euclidean algorithm finds the GCD much faster. It replaces the pair (a,b)(a, b) with the pair (b,amod⁡b)(b, a \bmod b), which has the same GCD, and repeats until the remainder is 0.

Why does the GCD stay the same? Any number that divides both aa and bb also divides a−qba - qb for any whole number qq. The remainder amod⁡ba \bmod b has this form, with qq equal to the number of full copies of bb in aa. So the common divisors of aa and bb are exactly the common divisors of bb and amod⁡ba \bmod b, and the largest one is the same for both pairs.

Why does it stop? A remainder is always smaller than the divisor, so the numbers shrink at every step. Whole numbers cannot shrink forever, so the remainder must reach 0 after a finite number of steps. When the remainder is 0, the last nonzero number is the answer, since gcd⁡(a,0)=a\gcd(a, 0) = a. Euclid described this method in Book VII of his Elements, written around 300 BC.

Why is it fast? The number of division steps is at most about five times the number of digits in the smaller number, so two 100-digit numbers need at most about 500 steps. Listing the divisors of a 100-digit number would take far longer. The method works for any two whole numbers, and the special case of 0 and 0 is covered in the FAQ below.

An everyday picture

Imagine two paper strips, 48 cm and 18 cm long. Cut as many 18 cm pieces as you can from the long strip, and 12 cm is left over. Now the strips are 18 cm and 12 cm. Cut 12 cm from the 18 cm strip, and 6 cm is left. The 6 cm piece fits into the 12 cm strip exactly twice, so 6 cm is the longest piece that measures both strips evenly.

Worked example: GCD of 48 and 18

We want the greatest common divisor of a=48a = 48 and b=18b = 18. That is the longest length that cuts both 48 and 18 into whole pieces with nothing left over. The algorithm repeats the remainder rule until the remainder is 0.

  1. Divide the larger number by the smaller. We divide 48 by 18: 48=2⋅18+1248 = 2 \cdot 18 + 12. Two full groups of 18 make 36, and 12 is left over, so 48mod⁡18=1248 \bmod 18 = 12. The rule says gcd⁡(48,18)=gcd⁡(18,12)\gcd(48, 18) = \gcd(18, 12), so we can work with the smaller pair instead.
  2. Repeat with the new pair. Now we divide 18 by 12: 18=1⋅12+618 = 1 \cdot 12 + 6. One full group of 12 leaves 6, so 18mod⁡12=618 \bmod 12 = 6. The rule gives gcd⁡(18,12)=gcd⁡(12,6)\gcd(18, 12) = \gcd(12, 6). The numbers keep getting smaller, which is why the process must end.
  3. Repeat once more. Divide 12 by 6: 12=2⋅6+012 = 2 \cdot 6 + 0. Two full groups of 6 make exactly 12, so the remainder is 0. The rule gives gcd⁡(12,6)=gcd⁡(6,0)\gcd(12, 6) = \gcd(6, 0).
  4. Stop when the remainder is 0. With the pair (6,0)(6, 0), the rule gcd⁡(a,0)=a\gcd(a, 0) = a says the answer is the first number, 6. The last nonzero remainder before 0 is the answer, because it is the largest number that fits into both earlier numbers with nothing left over.
  5. Check that 6 really fits both numbers. 48÷6=848 \div 6 = 8 and 18÷6=318 \div 6 = 3. Both results are whole numbers with nothing left over, so 6 is a common divisor. The algorithm guarantees that no larger common divisor exists.
  6. Use the answer to simplify a fraction. Dividing the top and the bottom of 4818\frac{48}{18} by 6 gives 83\frac{8}{3}. Both numbers shrank by the same factor, so the fraction keeps the same value, about 2.67.
  7. Count the work. The algorithm used 3 division steps. Listing every divisor of 48 and of 18, and then finding the biggest one that appears in both lists, would take much longer even for numbers this small.

Answer: The greatest common divisor of 48 and 18 is 6. The algorithm needed 3 division steps and never listed a single divisor.

The numbers in this example are recomputed by script every time the site is built.

Common mistakes

Where you will meet it

Questions people ask

Why does the algorithm stop when the remainder is 0?

When the remainder is 0, the last divisor divides the number before it with nothing left over. That divisor is the largest common divisor, and gcd⁡(a,0)=a\gcd(a, 0) = a records that final step.

What is the GCD of 0 and 0?

Every number divides 0, so there is no largest common divisor to pick. By convention the GCD of 0 and 0 is defined as 0.

Is the algorithm fast for huge numbers?

Yes. The number of steps grows with the number of digits, not with the size of the numbers. Even 100-digit numbers need at most about 5×100=5005 \times 100 = 500 division steps.

Sources

  1. Euclidean algorithm, Wikipedia
  2. Euclid's Elements, Wikipedia

| All Computer Science formulas |

Published 2026-10-22. Plain-text copy for AI agents and readers: computer-science/euclidean-algorithm-gcd.md.