The formula
Euclidean algorithm: gcd(a, b) = gcd(b, a mod b), repeated until the remainder is 0
LCM(a, b) = a × b ÷ gcd(a, b)
For more numbers: gcd(a, b, c) = gcd(gcd(a, b), c); lcm(a, b, c) = lcm(lcm(a, b), c)
With prime factors: GCD uses the lowest power of each shared prime, LCM the highest power of every prime
Worked example
- GCD of 84 and 120 by the Euclidean algorithm:
- 120 = 1 × 84 + 36; 84 = 2 × 36 + 12; 36 = 3 × 12 + 0, so the GCD is 12.
- LCM = 84 × 120 ÷ 12 = 840.
- With prime factors: 84 = 2² × 3 × 7 and 120 = 2³ × 3 × 5, so GCD = 2² × 3 = 12 and LCM = 2³ × 3 × 5 × 7 = 840.
GCD and LCM explained
The greatest common divisor (GCD) of two or more numbers is the largest number that divides all of them exactly. It's also called the highest common factor (HCF), mainly in the UK and India, or the greatest common factor (GCF) in many US textbooks. The least common multiple (LCM) is the smallest positive number that all of them divide into. For 84 and 120, the GCD is 12 and the LCM is 840. For two numbers, the GCD and LCM always multiply to give the product of the numbers.
The Euclidean algorithm
The Euclidean algorithm, described by Euclid around 300 BC, is one of the oldest algorithms still in everyday use. To find gcd(a, b), divide the larger number by the smaller and keep the remainder; then replace the larger number with the smaller and the smaller with the remainder, and repeat. When the remainder reaches zero, the last non-zero remainder is the GCD. It's very fast even for huge numbers, which is why computers use it rather than factorizing. The calculator shows each step for two numbers.
Using prime factors
The prime factor method is often taught first because it shows why the answers work. Write each number as a product of primes. The GCD takes every prime common to all the numbers, raised to the smallest power it appears with; the LCM takes every prime that appears in any of the numbers, raised to the largest power. A Venn diagram of the factors makes this clear: the overlap gives the GCD, and everything together gives the LCM. Our prime factorization calculator breaks down larger numbers for you.
Simplifying fractions and ratios
Dividing the numerator and denominator of a fraction by their GCD gives its simplest form in one step: 84/120 becomes 7/10 after dividing by 12. The same works for ratios, such as reducing 84 : 120 to 7 : 10. When adding fractions, the LCM of the denominators is the lowest common denominator: for 1/84 + 1/120, use 840. The fraction calculator and ratio calculator apply these steps automatically and show the working.
Real-world LCM problems
LCM answers questions about when repeating events line up. If one bus comes every 12 minutes and another every 18 minutes, they arrive together every LCM(12, 18) = 36 minutes. Gears with 24 and 36 teeth return to the same position after LCM(24, 36) = 72 teeth have passed, which is 3 turns of the small gear and 2 of the large. GCD answers questions about splitting into equal groups: 84 apples and 120 oranges can be packed into at most 12 identical bags, each with 7 apples and 10 oranges.
GCD in programming
Most programming languages include a GCD function, such as Python's math.gcd, and computing LCM as a ÷ gcd(a, b) × b avoids overflow.
Coprime numbers
Two numbers are coprime, or relatively prime, when their GCD is 1, meaning they share no prime factors, like 8 and 15. Coprime numbers matter in cryptography, where RSA chooses an exponent coprime to a related number, in gear design, where coprime tooth counts spread wear evenly, and in music and rhythm, where coprime cycle lengths create patterns that take a long time to repeat. For modular arithmetic with large numbers, see the modulo calculator.