All Tools

GCD and LCM Calculator with Euclidean Steps

Find the greatest common divisor and least common multiple of two or more integers of any size, with every Euclidean algorithm step and Bézout coefficients. Exact, free and private in your browser.

Show
Greatest common divisor (GCD)
21
Least common multiple (LCM)
23562
Bézout identity
1071 × (-3) + 462 × 7 = 21


About the GCD and LCM Calculator

The GCD and LCM Calculator finds the greatest common divisor and the least common multiple of two or more integers and shows how it got there. The greatest common divisor, also called the greatest common factor (GCF) or highest common factor (HCF), is the largest whole number that divides every input exactly. The least common multiple is the smallest positive number that every input divides. For two integers the page also gives the Bézout coefficients, two integers x and y with a·x + b·y = gcd(a, b).

Type the integers separated by commas, spaces or new lines. Negative numbers and zero are accepted, and the integers can be as long as 1,000 digits each, with up to 100 of them. Arithmetic is exact, so a 300-digit result comes out digit for digit. Everything runs in your browser and the numbers you type are never sent anywhere.

How to use it

Steps

  1. Choose GCD or LCM at the top. The chosen result is shown first and its working is listed below it.
  2. Type or paste the integers, for example 1071, 462 or one integer per line.
  3. Read the results as you type. The steps box has Copy and Download .txt buttons, so the working can go straight into homework, a report or a code comment.

Limits

At most 100 integers of up to 1,000 digits each are accepted. Decimals and fractions are refused with a message naming the item that is not a whole number.

How the answers are found

The Euclidean algorithm repeatedly divides the larger number by the smaller and keeps the remainder. When the remainder reaches zero, the last non-zero remainder is the greatest common divisor. For 1071 and 462 the page shows:

1071=2×462+147462=3×147+21147=7×21+0\begin{aligned} 1071 &= 2 \times 462 + 147 \\ 462 &= 3 \times 147 + 21 \\ 147 &= 7 \times 21 + 0 \end{aligned}

so gcd(1071, 462) = 21. The number of steps grows only with the number of digits, which is why even 1,000-digit integers finish at once.

The least common multiple follows from the greatest common divisor:

lcm⁡(a,b)=∣a×b∣gcd⁡(a,b)\operatorname{lcm}(a, b) = \frac{|a \times b|}{\gcd(a, b)}

For 12 and 18 that is 216 / 6 = 36. If any input is zero, the least common multiple is 0.

More than two integers are folded one at a time: gcd(12, 18, 30) = gcd(gcd(12, 18), 30) = gcd(6, 30) = 6, and in the same way lcm(12, 18, 30) = lcm(36, 30) = 180. The steps box shows each stage.

Bézout coefficients come from the extended Euclidean algorithm, which keeps track of how each remainder is built from the two inputs. For 240 and 46 it gives 240 × (−9) + 46 × 47 = 2. These coefficients are what you need to find a modular inverse: when gcd(a, m) = 1, the coefficient of a is its inverse modulo m.

Worked examples

  • gcd(48, 36) is 12 and lcm(48, 36) is 144.
  • 35 and 64 are coprime: their greatest common divisor is 1, and the page says so.
  • gcd(−12, 0) is 12, because every integer divides 0, and lcm(−12, 0) is 0.
  • Fractions: to simplify 462/1071, divide both parts by gcd = 21 to get 22/51.

Privacy

Every calculation runs in your browser. Nothing is uploaded, and nothing is kept once you close the tab.

Related tools

To see the prime factors behind a greatest common divisor, use the Prime Factorization Calculator: the GCD is the product of the shared prime powers, and the LCM is the product of the highest powers. The Prime Number Generator lists and tests primes, and the Number List Calculator adds up and averages a list of numbers.

Open-source credits

  • The calculator is written for this site and uses no third-party library. Arithmetic uses the browser's built-in BigInt, specified in ECMA-262.
  • The greatest common divisor uses the Euclidean algorithm from Euclid's Elements, book VII, propositions 1 and 2, and the Bézout coefficients use the extended Euclidean algorithm as given in Donald Knuth's The Art of Computer Programming, volume 2, section 4.5.2. The worked example 1071 and 462 is the standard textbook case.

Frequently asked questions

Are the GCD, GCF and HCF the same thing?

Yes. Greatest common divisor, greatest common factor and highest common factor are three names for the largest whole number that divides every one of the integers without a remainder.

How do I find the GCD of more than two numbers?

Type all of them, separated by commas, spaces or new lines. The calculator folds them one at a time, gcd(gcd(a, b), c), and lists the Euclidean steps of every stage in the steps box.

What are Bézout coefficients used for?

They are two integers x and y with a·x + b·y = gcd(a, b). When the gcd is 1, x is the inverse of a modulo b, which is how modular inverses are found in RSA and in solving linear congruences.

What is the LCM used for in everyday maths?

The least common multiple is the lowest common denominator when adding fractions, and it answers timing questions such as when two buses that leave every 12 and 18 minutes next depart together: after 36 minutes.

What happens with zero or negative numbers?

Signs are ignored, so the gcd is never negative, and gcd(a, 0) is the size of a because every integer divides zero. Any least common multiple that involves zero is 0.

Other related tools