Greatest GCD between some numbers
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If you want the GCD of several numbers, the task is simpler than it looks: repeatedly apply the two-number GCD until only one value remains. The key insight is that GCD is associative, so gcd(a, b, c) is the same as gcd(gcd(a, b), c).
Use the Euclidean algorithm as the core step
The fastest standard way to compute the GCD of two integers is the Euclidean algorithm. It repeatedly replaces the pair (a, b) with (b, a % b) until b becomes zero.
This prints 6.
The algorithm is efficient because each step shrinks the problem substantially. That is why almost every language standard library uses some version of it.
Extend it to many numbers by reduction
To find the GCD of a whole list, reduce the list pair by pair.
This works because:
- '
gcd(84, 126)is42' - '
gcd(42, 210)is also42'
So the greatest common divisor of the whole set is 42.
In languages that already provide a built-in GCD, you can still use the same reduction strategy.
Prime factorization gives the same answer conceptually
If you already know the prime factorizations, the GCD is the product of the common primes raised to the smallest exponent shared by all numbers.
For example:
- '
60 = 2^2 * 3^1 * 5^1' - '
72 = 2^3 * 3^2'
The shared primes are 2 and 3. Keep the lower exponent for each:
- '
2^2' - '
3^1'
So the GCD is 12.
This is a nice explanation of why the GCD works, but it is usually not the best way to compute it in code unless the factorization is already available for some other reason.
Handle edge cases deliberately
A few cases matter in real implementations:
- '
gcd(a, 0)isabs(a)' - '
gcd(0, 0)is usually defined as0in programming libraries' - negative signs do not affect the answer, so use absolute values
That is why the example code normalizes inputs with abs before iterating.
If your problem is actually "find the largest pairwise GCD among some numbers", that is a different task. The simple reduction approach gives the GCD shared by the entire set, not the best GCD obtainable from just one pair or subset.
Why GCD matters in practice
GCD is not only a classroom exercise. It shows up in:
- fraction simplification
- modular arithmetic and cryptography
- checking whether numbers are coprime
- normalizing ratios
- number-theory algorithms such as LCM computation
For example, the least common multiple can be computed from the GCD:
That connection is one reason efficient GCD code appears so often in algorithm libraries.
Common Pitfalls
- Confusing "GCD of the whole set" with "largest GCD among some pair or subset".
- Using prime factorization in code when the Euclidean algorithm would be much simpler and faster.
- Forgetting to normalize negative inputs and then being surprised by sign behavior.
- Ignoring edge cases involving zero.
- Recomputing GCDs naively with trial division instead of using Euclid's algorithm.
Summary
- The GCD of several numbers can be computed by repeatedly applying the two-number GCD.
- The Euclidean algorithm is the standard fast method for each pairwise step.
- Prime factorization explains the result conceptually but is usually not the best runtime method.
- Zero and negative values should be handled explicitly in real code.
- Be clear whether you want the GCD of the whole set or the best GCD from only part of it.
Related reading
- Greatest linear dimension 2d set of points
- Heap's algorithm for permutations
- Hexagonal Grids, how do you find which hexagon a point is in?
- Highest Valued Palindrome
- Hilbert sort by divide and conquer algorithm?
- How a sequence of numbers can be converted to a single number?
- How can a transform a polynomial to another coordinate system?
- How can I convert from degrees to radians?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.