Algorithms: Euclid and the GCD
Algorithms:Euclid andthe GCD
An algorithm is a recipe exact enough for a machine. One of the oldest still in use was written down by Euclid around 300 BC.
Slide 1 of 15: Algorithms: Euclid and the GCD.
Algorithms:Euclid andthe GCD
An algorithm is a recipe exact enough for a machine. One of the oldest still in use was written down by Euclid around 300 BC.
An algorithm is a finite list of exact steps that always finishes with an answer.
The greatest common divisor
- Divisors of 48
- 1, 2, 3, 4, 6, 8, 12, 16, 24, 48
- Divisors of 18
- 1, 2, 3, 6, 9, 18
- GCD(48, 18)
- 6
The greatest common divisor of two whole numbers is the largest number that divides both with nothing left over. In school it is often called the greatest common factor, GCF.
Fractions use it: 18/48 simplifies to 3/8 by dividing top and bottom by 6.
Listing divisors works. For two 20-digit numbers it can mean 10²⁰ tests: over 3,000 years at a billion tests a second.
Euclid needs at most 100 steps for the same pair.
Euclid’s idea
Any number that divides both a and b also divides their difference, and so also the remainder a mod b.
That means GCD(a, b) = GCD(b, a mod b). The numbers get smaller every step.
When the remainder reaches 0, the last nonzero number is the answer.
Euclid in lengths
- 1Euclid thought in lengths. Start with a 48 by 18 rectangle.
- 2Cut off the largest squares you can: two of side 18. A 12 by 18 strip is left.
- 3From that strip, one square of side 12. A 6 by 12 strip is left.
- 4Two squares of side 6 fill it exactly. Nothing is left over.
- 5A 6 by 6 square tiles the whole rectangle, and no larger square does. GCD(48, 18) = 6.
“The Euclidean algorithm is the granddaddy of all algorithms, because it is the oldest nontrivial algorithm that has survived to the present day.”
A trace: GCD(1071, 462)
- 1Divide 1071 by 462: 2 times, remainder 147.
- 2Now divide 462 by 147: 3 times, remainder 21.
- 3Divide 147 by 21: exactly 7 times, remainder 0.
- 4The remainder is 0, so the answer is the last divisor: 21.
| a | q | b | r | |||
|---|---|---|---|---|---|---|
| 1071 | = | 2 | × | 462 | + | 147 |
| 462 | = | 3 | × | 147 | + | 21 |
| 147 | = | 7 | × | 21 | + | 0 |
Remainder 0 · GCD(1071, 462) = 21
Java · Lines 1 to 21 of 38
The program
This Java program, shared on AI Academy’s computer science page, asks for two numbers and runs Euclid’s loop.
1//The following code takes in two numbers and finds the GCF using the Euclidean Algorithm2import java.util.Scanner;3public class EuclideanAlgorithm {4public static void main(String[] args) {5Scanner myScanner = new Scanner(System.in);6System.out.println("What is the first number?");7int a = myScanner.nextInt();8System.out.println("What is the second number?");9int b = myScanner.nextInt();10int gcd = 0;11if(a > b) {12int r = a%b;13while(r > 0) {14a = b;15b = r;16r = a%b;17}18if(r == 0) {19gcd = b;20}21}Java · Lines 22 to 38 of 38
The program, continued
It keeps the larger number first, so it has two mirrored branches.
22else if(b > a){23int r = b%a;24while(r > 0) {25b = a;26a = r;27r = b%a;28}29if(r == 0) {30gcd = a;31}32}33else {34gcd = a;35}36System.out.println("The GCD is: " + gcd);37}38}Reading the loop
- %
- Remainder after division
- Loop ends
- When r == 0
If a is larger, r = a % b is the remainder. While r is above 0, the pair shifts: a takes b’s value, b takes r, and a new remainder is computed.
When r reaches 0, b holds the GCD. The else if branch does the same with the roles of a and b swapped.
If the two numbers are equal, the number itself is the answer. The program assumes two positive whole numbers.
Why it is fast
Every two steps, the larger number at least halves. So the number of steps grows with the number of digits, not with the size of the numbers.
In 1844 Gabriel Lamé proved the number of steps is never more than five times the number of decimal digits in the smaller number. The worst cases are neighbouring Fibonacci numbers.
Try it
- 1By handTrace GCD(252, 105) the way we traced 1071 and 462.
- 2Break itWhat does the program do if one of the numbers is 0? Look closely at a%b. Then try a negative pair and see what it prints.
- 3Shorten itRewrite it as a recursive method: gcd(a, b) is a when b is 0, and gcd(b, a % b) otherwise.
- 4CountAdd a counter to the loop. Which pairs under 100 take the most steps?
Video · CS50
Thinking algorithmically
The first lecture of Harvard’s CS50: binary, algorithms and how to judge which one is faster.

Go further
Courses, contests and practice sets for students who want more.
- Java code for GCF using Euclidean AlgorithmFile · AI Academy
- CS50x: Introduction to Computer ScienceCourse · Harvard University (opens in a new tab)
- USACO GuideSite · USACO Guide team (opens in a new tab)
- American Computer Science LeagueSite · ACSL (opens in a new tab)
- CSES Problem SetSite · University of Helsinki (opens in a new tab)
End of the deckNext deck: How a neural network learns