Compute the greatest common divisor (GCD) and least common multiple (LCM) of any list of integers. Useful for simplifying fractions, finding common denominators, solving diophantine equations, and anywhere in mathematics where you need a shared factor or period.
GCD via the Euclidean algorithm
GCD is computed using the ancient Euclidean algorithm, which exploits the identity gcd(a, b) = gcd(b, a mod b). Starting from two numbers, you repeatedly replace the larger one with the remainder of dividing it by the smaller, until one of them hits zero. The other one is then the GCD. The algorithm terminates in O(log(min(a, b))) steps — fast enough to handle integers up to JavaScript’s Number.MAX_SAFE_INTEGER (about 9 × 10¹⁵) without breaking a sweat.
For lists of more than two integers, the tool reduces pairwise: gcd(a, b, c) = gcd(gcd(a, b), c). Order doesn’t matter because GCD is associative.
LCM from GCD
The tool computes LCM using the identity lcm(a, b) = |a × b| / gcd(a, b). Compute the GCD first, then divide the product of the two inputs by it. This is faster than computing LCM directly (which would require factoring), and uses GCD which you’re computing anyway. The tool divides before multiplying where possible to keep intermediate values smaller, which matters for large inputs approaching the safe-integer boundary.
Like GCD, LCM generalises to lists via pairwise reduction.
Example: simplifying a fraction
Simplifying 12/18 to its lowest terms: GCD(12, 18) = 6, so divide top and bottom by 6 → 12/6 = 2, 18/6 = 3, giving 2/3. Any fraction a/b simplified to lowest terms is a/gcd(a, b) over b/gcd(a, b). The fraction calculator does this simplification as part of any arithmetic operation on fractions.
Example: common denominator
Adding 1/4 + 1/6. Common denominator = LCM(4, 6) = 12. Rewrite: 3/12 + 2/12 = 5/12. The LCM gives you the smallest denominator where both fractions share the same base, which keeps the arithmetic clean and the result in lowest terms (or close to it).
Example: scheduling two events
Event A happens every 4 days, event B every 6 days. Both happen together every LCM(4, 6) = 12 days. If event C happens every 8, all three align every LCM(4, 6, 8) = 24 days. LCM is the standard tool for any “when does this cycle repeat?” question.
Example: coprime detection
Two integers are coprime (share no common factor other than 1) if and only if GCD = 1. GCD(14, 15) = 1 → coprime. GCD(14, 21) = 7 → not coprime. The tool doesn’t have a dedicated “coprime yes/no” output, but the GCD value itself answers the question.
Why not factorisation
A naive approach to GCD is to factor both numbers, find shared prime factors, and multiply. That works for small numbers but scales badly — factoring is computationally hard, which is literally what RSA encryption relies on. The Euclidean algorithm sidesteps factoring entirely and runs in logarithmic time regardless of whether the inputs are primes, composites, or giant numbers. For any serious GCD computation, Euclid beats factorisation hands down.
What this tool does not do
It doesn’t handle non-integers. Rationals and irrationals don’t have GCD or LCM in the usual sense; the tool rejects fractional and non-integer inputs with a parse error.
It doesn’t show the factorisation of your inputs. “What are the prime factors of 60?” is a different question — use the prime number checker which includes a factorisation mode. The tool just returns GCD and LCM.
It doesn’t support integers larger than 2⁵³. JavaScript numbers are double-precision floats with 53 bits of integer precision, so inputs beyond about 9 × 10¹⁵ lose precision. For arbitrary-precision GCD / LCM, you need a library that supports BigInt (like JavaScript’s built-in BigInt type, or a dedicated big-number package).
It doesn’t compute Bézout coefficients. For ax + by = gcd(a, b), the extended Euclidean algorithm finds x and y too — useful for modular inverses and Diophantine equations. The tool runs the basic Euclidean algorithm only.
It doesn’t display the step-by-step Euclidean algorithm. For pedagogical purposes showing “48 = 2×18 + 12, then 18 = 1×12 + 6, then 12 = 2×6 + 0” would be useful, but the tool just returns the final answer. For the walkthrough, a textbook or a dedicated step-by-step tool is the right resource.