GCD
Greatest Common Divisor
Mathematics
Number Theory
Math Problems

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.

Practice algorithms

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.

python
1def gcd(a, b):
2    a, b = abs(a), abs(b)
3    while b != 0:
4        a, b = b, a % b
5    return a
6
7print(gcd(48, 18))

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.

python
1from functools import reduce
2
3def gcd(a, b):
4    a, b = abs(a), abs(b)
5    while b != 0:
6        a, b = b, a % b
7    return a
8
9numbers = [84, 126, 210]
10result = reduce(gcd, numbers)
11
12print(result)

This works because:

  • 'gcd(84, 126) is 42'
  • 'gcd(42, 210) is also 42'

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) is abs(a)'
  • 'gcd(0, 0) is usually defined as 0 in 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:

python
1def lcm(a, b):
2    return abs(a * b) // gcd(a, b)
3
4print(lcm(12, 18))

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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.