Integer math

GCD and LCM Calculator

Find the greatest common divisor and least common multiple of a list of integers.

Enter integers to find their GCD and LCM

Separate integers with commas, spaces, or new lines. A comma always separates values: 1,000 means 1 and 0. Enter 1000 for one thousand. Empty comma entries are not allowed.

2–20 integers, up to 100 digits each and 4,096 characters in total. Optional + or - signs are accepted; decimals and exponents are not.

Enter adds a new line. Press Ctrl+Enter or ⌘+Enter to calculate.

Exact integer results

Enter at least two integers, then select Calculate.

Calculated in your browser using exact integer arithmetic. Signs are removed before calculating GCD and LCM.

Worked examples

Several integers

12, 18, and 30

GCD = 6 · LCM = 180

The first pair gives GCD(12, 18) = 6 and LCM(12, 18) = 36. Then GCD(6, 30) = 6, and LCM(36, 30) = 180. The GCD divides all three inputs, and each input divides the LCM.

Euclidean algorithm

48 and 18

48 = 18 × 2 + 12
18 = 12 × 1 + 6
12 = 6 × 2 + 0

The final nonzero divisor is 6, so the GCD is 6. The LCM is (48 ÷ 6) × 18 = 144.

Signs and zero

−12, 0, and 18

GCD(12, 0, 18) = 6 · LCM = 0

The negative sign does not affect the result. Every positive integer divides zero, so zero leaves the nonzero values' GCD unchanged. The calculator returns 0 for the LCM whenever zero appears.

Method, exactness, and zero conventions

After converting both inputs to absolute values, the Euclidean algorithm repeatedly uses GCD(a, b) = GCD(b, a mod b). When b = 0, the result is a. This is the remainder-based method described in NIST's definition of Euclid's algorithm.

For two nonzero integers, LCM(a, b) = |(a ÷ GCD(a, b)) × b|. The division is exact and is performed before multiplication. If either input is zero, the calculator returns zero directly instead of dividing by a possibly zero GCD.

For a longer list, first combine the first two values, then combine their GCD and LCM separately with each following value. For example, the third value is combined with the previous GCD to obtain the next GCD, and with the previous LCM to obtain the next LCM.

An all-zero list has no greatest positive common divisor under that wording. This calculator adopts GCD(0, 0, …) = 0 and LCM(a, 0, …) = 0, consistent with the conventions documented for Python's GCD and LCM functions. The implementation here uses browser JavaScript integer arithmetic.

All calculations use exact integers. No decimal approximation or rounding is involved. Each input can have up to 100 digits, excluding its sign; leading zeros count toward that limit. The list is limited to 20 integers and 4,096 characters in total. Results can be much longer than the inputs and are displayed in full.

The worked division list shows all steps when there are at most 24. For a longer process, it shows the first 12 and last 12, with the number omitted. Every step is still used to compute the result. The reduction trail has at most 19 rows and abbreviates long intermediate values for readability.

Common questions

Are GCD, GCF, and HCF the same thing?

For integers, greatest common divisor, greatest common factor, and highest common factor name the same result. For example, 12 and 18 share positive divisors 1, 2, 3, and 6, so their GCD, GCF, or HCF is 6.

Can I enter negative numbers?

Yes. Optional keyboard + and - signs are accepted. The calculation uses absolute values: −12 and 18 produce the same GCD 6 and LCM 36 as 12 and 18. Returned results are never negative.

How are zeros handled?

If at least one input is nonzero, the GCD is the GCD of the nonzero absolute values; for example, GCD(0, 18) = 18. The GCD of an all-zero list is defined as 0 for this tool. Any zero makes the LCM 0. These zero cases are conventions, rather than claims that zero is a greatest positive divisor or a smallest positive multiple.

Can I use commas to group thousands?

No. Commas always separate entries. Typing 1,000 produces two integers, 1 and 0, whose GCD is 1 and LCM is 0. To enter one thousand, use 1000. Spaces and line breaks also separate entries, so do not group a single number with spaces.

Can I enter decimals, fractions, or scientific notation?

No. Enter whole integers such as 12, −18, or 1000. Values such as 12.0, 1/2, and 1e3 are rejected. To simplify a ratio that includes decimal parts, use the Ratio Calculator instead.

Why can the LCM be much larger than any input?

The common multiple must include every required factor. If two positive integers have GCD 1, their LCM is their product. Combining many integers can therefore produce a very long result. The main result is kept exact and is not shortened; only intermediate-table and recent-history previews can use an ellipsis.