Skip to main content
Online
Number theory · step-by-step

GCD and LCM Calculator

Enter two or more whole numbers. The solver runs the Euclidean algorithm line by line, finds the LCM, and for two numbers also gives the Bézout identity from the extended algorithm.

Two or more integers. Large numbers are handled exactly.

Complete guide

Greatest common divisor and least common multiple: the Euclidean algorithm explained

4 min read Reviewed By the GetCalcMaster team · Editorial policy

The greatest common divisor (GCD, also called the highest common factor) of two integers is the largest number that divides both. The least common multiple (LCM) is the smallest positive number both divide into. GCDs simplify fractions; LCMs give common denominators and solve scheduling problems (“when will two cycles line up again?”).

The solver uses the Euclidean algorithm, one of the oldest algorithms still in daily use (described by Euclid around 300 BC), and the extended version that also produces Bézout coefficients.

Key formulas

Euclidean algorithm
gcd(a, b) = gcd(b, a mod b), stop when the remainder is 0
The last non-zero remainder is the GCD.
LCM from GCD
lcm(a, b) = |a·b| / gcd(a, b)
Compute the GCD first; it is far faster than listing multiples.
Bézout's identity
a·x + b·y = gcd(a, b) for some integers x, y
Found by the extended Euclidean algorithm; used for modular inverses in cryptography.
Via prime factorizations
GCD: lowest powers of shared primes · LCM: highest powers of all primes
252 = 2²·3²·7 and 198 = 2·3²·11 → GCD = 2·3² = 18.

Worked examples

Example 1: GCD of 252 and 198

Find gcd(252, 198).

  1. 252 = 1 × 198 + 54
  2. 198 = 3 × 54 + 36
  3. 54 = 1 × 36 + 18
  4. 36 = 2 × 18 + 0
Answer: GCD = 18, so LCM = 252 × 198 / 18 = 2,772.
Example 2: Simplifying a fraction

Simplify 1071/462.

  1. 1071 = 2 × 462 + 147; 462 = 3 × 147 + 21; 147 = 7 × 21 + 0.
  2. GCD = 21: 1071/21 = 51, 462/21 = 22.
Answer: 51/22.
Example 3: When do two buses meet again?

Bus A leaves every 12 minutes and bus B every 18 minutes, both at 8:00.

  1. lcm(12, 18) = 12 × 18 / gcd(12, 18) = 216 / 6 = 36.
Answer: They next leave together at 8:36.
Example 4: Modular inverse

Find the inverse of 17 modulo 31.

  1. Extended Euclid gives 17 × 11 + 31 × (−6) = 1.
  2. So 17 × 11 ≡ 1 (mod 31).
Answer: 17⁻¹ ≡ 11 (mod 31).
Example 5: Adding fractions with the LCM

Add 5/12 + 7/18.

  1. LCM(12, 18) = 12 × 18 ÷ GCD(12, 18) = 216 ÷ 6 = 36.
  2. Rewrite: 5/12 = 15/36 and 7/18 = 14/36.
  3. Add the numerators: 29/36, which cannot be simplified (GCD(29, 36) = 1).
Answer: 29/36 ≈ 0.806
Try it: 12, 18
Example 6: Largest square tiles for a rectangle

A panel is 84 cm by 126 cm. What is the largest square tile that covers it exactly, and how many are needed?

  1. The tile side must divide both lengths, so take the GCD: GCD(84, 126) = 42 cm.
  2. Tiles along each side: 84 ÷ 42 = 2 and 126 ÷ 42 = 3.
Answer: 42 cm tiles, 2 × 3 = 6 of them.
Try it: 84, 126

Why the Euclidean algorithm works

Any number that divides both a and b also divides a − qb, the remainder. So the pair (a, b) and the pair (b, a mod b) have exactly the same common divisors, and therefore the same greatest one. The numbers shrink quickly — at least by half every two steps — which is why the algorithm handles numbers with hundreds of digits instantly.

GCD or LCM? A quick way to decide

  • Splitting things into equal groups with nothing left over, or the largest piece that fits several lengths → GCD.
  • Finding when repeating events next happen together, or a common denominator → LCM.
  • The two answers are linked: for two positive whole numbers, GCD × LCM = the product of the numbers.

Common mistakes to avoid

  • Confusing GCD with LCM (the GCD is never larger than the smaller number; the LCM is never smaller than the larger).
  • Multiplying two numbers to get the LCM when they share factors.
  • Stopping at a zero remainder and reporting 0 instead of the previous remainder.

Frequently asked questions

What is the GCD of a number and 0?
gcd(a, 0) = |a|, because every integer divides 0.
What does it mean if the GCD is 1?
The numbers are coprime (relatively prime). Their LCM is then simply their product.
Can I find the GCD of more than two numbers?
Yes. The solver applies gcd(gcd(a, b), c) and so on, showing each Euclidean run.
Why does GCD × LCM equal the product of the two numbers?
In terms of prime factors, the GCD takes the smaller exponent of each prime and the LCM the larger one. Smaller plus larger is the sum of both exponents, so multiplying GCD and LCM gives back the product. This only holds for two numbers, not three or more.
Can the LCM be smaller than one of the numbers?
No. For positive whole numbers the LCM is a multiple of each number, so it is at least as large as the larger one. It equals the larger number exactly when the smaller one divides it, as with 6 and 18.

Related tools and guides

Educational content. Results are computed in your browser with standard double-precision arithmetic; double-check anything with legal, financial, medical, or safety consequences. Spotted an error? Tell us and we will correct it.