How to Find the Greatest Common Divisor (GCD)

The greatest common divisor (GCD) of two or more integers is the largest integer that divides all of them exactly. This page explains three methods for finding GCD — listing factors, prime factorization, and the Euclidean algorithm — with worked examples for each.

Definition

GCD(a, b) is the largest positive integer d such that d divides a and d divides b with no remainder. The same concept is also called HCF (highest common factor) or GCF (greatest common factor).

GCD(48, 36) = 12, because 12 is the largest number that divides both 48 and 36 exactly. The divisors of 48 are {1, 2, 3, 4, 6, 8, 12, 16, 24, 48}; divisors of 36 are {1, 2, 3, 4, 6, 9, 12, 18, 36}. The largest common element is 12.

Method 1 — Listing Factors

List all factors of each number and find the largest one they share. This method is practical only for small numbers.

NumberFactors
481, 2, 3, 4, 6, 8, 12, 16, 24, 48
361, 2, 3, 4, 6, 9, 12, 18, 36
Common factors1, 2, 3, 4, 6, 12
GCD12

Method 2 — Prime Factorization

Write each number as a product of prime powers. The GCD is the product of shared primes, each raised to the minimum exponent.

Example: GCD(360, 756)

PrimeExponent in 360Exponent in 756min → used in GCD
2322
3232
5100 (excluded)
7010 (excluded)

Method 3 — Euclidean Algorithm

The Euclidean algorithm is the fastest method, especially for large numbers. It is based on the property that GCD(a, b) = GCD(b, a mod b).

  1. If b = 0, then GCD = a. Stop.
  2. Compute remainder r = a mod b.
  3. Set a = b and b = r.
  4. Repeat from step 1.

Worked Example: GCD(252, 105)

Stepaba = b × q + rRemainder r
1252105252 = 105 × 2 + 4242
210542105 = 42 × 2 + 2121
3422142 = 21 × 2 + 00

Remainder reaches 0. The GCD is the last non-zero remainder: GCD(252, 105) = 21.

Worked Example: GCD(1071, 462)

Stepaba = b × q + rRemainder r
110714621071 = 462 × 2 + 147147
2462147462 = 147 × 3 + 2121
314721147 = 21 × 7 + 00

GCD(1071, 462) = 21.

GCD of More Than Two Numbers

Apply GCD pairwise using the associative property:

Example: GCD(12, 18, 24) → GCD(GCD(12, 18), 24) = GCD(6, 24) = 6.

Key Properties of GCD

PropertyStatementExample
CommutativeGCD(a, b) = GCD(b, a)GCD(12, 8) = GCD(8, 12) = 4
AssociativeGCD(a, GCD(b, c)) = GCD(GCD(a, b), c)
IdentityGCD(a, 0) = aGCD(7, 0) = 7
GCD × LCMGCD(a, b) × LCM(a, b) = a × b4 × 24 = 8 × 12 = 96
CoprimeIf GCD(a, b) = 1, a and b are coprimeGCD(8, 15) = 1

Applications

  • Simplifying fractions: divide numerator and denominator by their GCD.
  • Solving linear Diophantine equations: ax + by = c has integer solutions if and only if GCD(a, b) divides c.
  • RSA cryptography: the extended Euclidean algorithm computes modular inverses for key generation.
  • Computer science: used in algorithms for least common multiple, modular arithmetic, and hash functions.
💡To simplify a fraction like 48/36: GCD(48, 36) = 12 → 48/36 = (48÷12)/(36÷12) = 4/3.

Frequently Asked Questions

What is the GCD of a number and itself?

GCD(a, a) = a. Any number divides itself, so it is its own GCD. For example, GCD(7, 7) = 7.

Why does the Euclidean algorithm work?

It works because GCD(a, b) = GCD(b, a mod b). Any common divisor of a and b also divides a mod b (since a mod b = a − b × ⌊a/b⌋). So the set of common divisors is unchanged at each step.

How fast is the Euclidean algorithm?

It runs in O(log min(a, b)) steps. The number of steps is at most 5 times the number of decimal digits in the smaller number (Lamé's theorem). In practice it is extremely fast even for very large integers.

What is the extended Euclidean algorithm?

The extended version also finds integers x and y such that ax + by = GCD(a, b). This is called Bézout's identity and is the foundation of modular inverse computation used in RSA encryption.

Is GCD defined for negative numbers?

By convention, GCD is defined for positive integers. When applied to negative numbers, it is typically defined as GCD(|a|, |b|) — the GCD of their absolute values.