GCD and LCM calculator

Find the greatest common divisor (GCD) and the least common multiple (LCM) of two or more whole numbers, with Euclid's algorithm and the prime

loading GCD and LCM calculator
About this tool

What is the GCD and the LCM?

The greatest common divisor (GCD, also called the highest common factor or HCF) of some whole numbers is the largest number that divides all of them with no remainder. The least common multiple (LCM) is the smallest positive number that all of them divide into. For 12 and 18 the GCD is 6 and the LCM is 36. You need the GCD to simplify a fraction or to cut a rectangle into the biggest possible equal squares, and the LCM to add fractions with different denominators, to find when two repeating events line up again, or to plan shifts that repeat every few days.

This GCD and LCM calculator takes two or more whole numbers (up to 50 numbers, each with up to 100 digits) and returns both results at once. It does not only give the answer: it shows Euclid's algorithm division by division and the prime factorization, so you can check the work or copy it into a homework answer. The arithmetic uses arbitrary-precision integers, so very large numbers are exact, with no rounding. Everything runs in your browser and nothing is sent anywhere.

The two numbers are linked by one formula: for two numbers a and b, GCD(a, b) × LCM(a, b) = a × b. The tool computes the LCM as a / GCD × b, which keeps the intermediate numbers small. For more than two numbers it folds the list: GCD(a, b, c) = GCD(GCD(a, b), c), and the same for the LCM. Note that the shortcut GCD × LCM = product only holds for two numbers, not for three or more.

How to find the GCD and LCM

  1. Type or paste the numbers in the box, separated by commas, spaces or line breaks. An example is loaded when the page opens, so you never start from an empty screen.
  2. Read the two big results: the GCD and the LCM. If the GCD is 1, the page tells you the numbers are coprime (they share no prime factor).
  3. Open "By prime factorization" to see each number split into primes. The GCD takes only the primes that appear in every number, each with its smallest exponent. The LCM takes every prime that appears in any number, each with its largest exponent.
  4. Look at "Euclid's algorithm" for the chain of divisions of the first two numbers. With more than two numbers the tool combines them one at a time: the GCD of the first two is combined with the third, and so on.
  5. Copy the result you need.

Euclid's algorithm, in one line: GCD(a, b) = GCD(b, a mod b), repeated until the remainder is 0. The last non-zero remainder is the GCD. It is more than 2,000 years old and still the fastest practical method, because it never needs to factor the numbers.

Examples

Two small numbers: 12 and 18

Factorization: 12 = 2² × 3 and 18 = 2 × 3². The common primes are 2 and 3 with the smallest exponents, so GCD = 2 × 3 = 6. All primes with the largest exponents: 2² × 3² = 36. Check: 6 × 36 = 216 = 12 × 18.

Three numbers: 12, 18 and 30

GCD = 6, LCM = 180. The factorization is 12 = 2² × 3, 18 = 2 × 3², 30 = 2 × 3 × 5. Common primes: 2 and 3, so the GCD is 2 × 3 = 6. All primes: 2² × 3² × 5 = 180.

Euclid's algorithm on 1071 and 462

1071 = 2 × 462 + 147; 462 = 3 × 147 + 21; 147 = 7 × 21 + 0. The last non-zero remainder is 21, so GCD = 21 and LCM = 1071 × 462 / 21 = 23,562.

Coprime numbers: 17 and 31

Both are prime, so the GCD is 1 and the LCM is their product, 527. The tool flags them as coprime.

Simplifying a fraction with the GCD

To simplify 48/180, find GCD(48, 180) = 12 (LCM = 720). Dividing top and bottom by 12 gives 4/15. For the full step-by-step with mixed numbers and decimals, use the fraction calculator.

Common mistakes

  • Mixing up GCD and LCM. The GCD is never larger than the smallest number; the LCM is never smaller than the largest. If your GCD is bigger than one of the inputs, something is wrong.
  • Using GCD × LCM = product with three numbers. It only works for two. For 4, 6 and 10 the GCD is 2 and the LCM is 60, but 2 × 60 = 120, not 240.
  • Picking the largest exponent for the GCD. The GCD uses the smallest exponent of the common primes; the LCM uses the largest of all primes.
  • Typing decimals or negatives. Only whole numbers are accepted. For decimals, scale them to integers first (1.5 and 2.25 become 150 and 225).
  • Zero. The GCD of 0 and 5 is 5, and the LCM of 0 and anything is 0 by convention. This is correct, not a bug.

FAQ

What is the difference between GCD, HCF and MDC?

Nothing: they are the same thing. GCD (greatest common divisor) is the usual name in the US, HCF (highest common factor) in the UK, and MDC (máximo divisor comum) in Portuguese and Spanish.

How do I find the LCM of fractions' denominators?

Enter the denominators, for example 4, 6 and 10, and read the LCM: 60. That is the least common denominator you need to add the fractions. The fraction calculator does this automatically and shows the steps.

Is the GCD of two prime numbers always 1?

Yes, when the primes are different. If both numbers are the same prime, the GCD is that prime. Use the prime number checker to confirm whether a number is prime.

How large can the numbers be?

Up to 100 digits each and 50 numbers per calculation. The result is exact.

What is the LCM used for in real life?

Schedules (a bus every 12 minutes and another every 18 minutes leave together again after 36 minutes), gear ratios, tiling, and any problem that asks when two cycles coincide. For ratio problems you may also like the ratio calculator.

GCD and LCM in code

JavaScript

const gcd = (a, b) => (b === 0n ? a : gcd(b, a % b));
const lcm = (a, b) => (a / gcd(a, b)) * b;
console.log(gcd(1071n, 462n)); // 21n
console.log(lcm(12n, 18n)); // 36n

Python

from math import gcd, lcm
print(gcd(12, 18, 30))  # 6
print(lcm(12, 18, 30))  # 180

Go

func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
// gcd(1071, 462) == 21; lcm(a, b) = a / gcd(a, b) * b
Report a problem