Math

GCD & LCM Calculator

Calculate the Greatest Common Divisor and Least Common Multiple of two or more integers. See the step-by-step Euclidean algorithm and prime factorization method.

Quick Answer

GCD is the largest positive integer that divides all given numbers. LCM is the smallest positive integer that is a multiple of all given numbers. They are related: GCD(a,b) × LCM(a,b) = |a × b|.

Enter Integers

Enter two or more integers, separated by commas or spaces. Non-zero values only.

Results

GCD (Greatest Common Divisor)
12
LCM (Least Common Multiple)
720

Euclidean Algorithm (for 48 and 36)

Step 1: 48 = 36 × 1 + 12
Step 2: 36 = 12 × 3 + 0
Last non-zero remainder: GCD = 12

Prime Factorization Method

48 = 2⁴ × 3
36 = 2² × 3²
60 = 2² × 3 × 5
GCD = product of minimum powers of common primes:
GCD = 2² × 3 = 12
LCM = product of maximum powers of all primes:
LCM = 2⁴ × 3² × 5 = 720

About This Tool

The GCD and LCM Calculator computes the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of two or more integers using two complementary methods: the Euclidean algorithm and prime factorization. It shows detailed step-by-step solutions for both approaches, making it an excellent learning tool for students studying number theory, discrete mathematics, or preparing for standardized tests. Understanding GCD and LCM is fundamental to working with fractions, modular arithmetic, and many areas of computer science.

The Euclidean Algorithm

The Euclidean algorithm is one of the oldest algorithms still in common use, dating back to Euclid's Elements (circa 300 BCE). It computes the GCD of two integers by repeatedly applying the division algorithm: divide the larger number by the smaller, then replace the larger with the smaller and the smaller with the remainder. When the remainder reaches zero, the last non-zero remainder is the GCD. The algorithm is remarkably efficient, running in O(log(min(a,b))) steps, which makes it practical even for very large numbers. The extended Euclidean algorithm also finds integers x and y such that ax + by = gcd(a,b), which is essential for computing modular inverses in cryptography.

Prime Factorization Method

The prime factorization method works by decomposing each number into its prime factors. The GCD is then the product of all common prime factors raised to their minimum power, and the LCM is the product of all prime factors raised to their maximum power. For example, if a = 2^3 times 3^2 and b = 2^2 times 3^3, then GCD = 2^2 times 3^2 = 36 and LCM = 2^3 times 3^3 = 216. While this method is more intuitive and educational, it requires finding the prime factorization of each number, which is computationally expensive for very large numbers. The Euclidean algorithm is preferred in practice for its efficiency.

The GCD-LCM Relationship

For any two positive integers a and b, the product of their GCD and LCM equals the product of the numbers themselves: GCD(a,b) times LCM(a,b) = a times b. This elegant relationship provides a quick way to compute the LCM once you know the GCD: LCM(a,b) = (a times b) / GCD(a,b). It also explains why numbers that share many common factors (large GCD) have a relatively small LCM, and vice versa. Coprime numbers (GCD = 1) have LCM equal to their product. This relationship extends to prime factorizations: taking the min of exponents (GCD) and the max (LCM) for the same set of primes naturally satisfies this identity.

Applications of GCD

The GCD has numerous practical applications. In fraction simplification, dividing the numerator and denominator by their GCD gives the fraction in lowest terms. In cryptography, the extended Euclidean algorithm computes modular inverses needed for RSA encryption. In music theory, the GCD of two frequencies determines their interval relationship. In tiling problems, the GCD of two dimensions determines the largest square tile that can perfectly cover a rectangle. Computer scientists use GCD in the Stern-Brocot tree for efficient rational number representation and in algorithms for computing continued fractions.

Applications of LCM

The LCM is used whenever you need to synchronize cycles or find common denominators. Adding fractions with different denominators requires finding the LCM of the denominators (the least common denominator). In scheduling, the LCM tells you when two periodic events will next coincide: if one event occurs every 12 days and another every 18 days, they coincide every LCM(12,18) = 36 days. In gear systems, the LCM of tooth counts determines when the gears return to their starting position. In number theory, the LCM appears in the Chinese Remainder Theorem and Carmichael's function.

Extending to Multiple Numbers

Both GCD and LCM extend naturally to more than two numbers. GCD(a,b,c) = GCD(GCD(a,b), c), and similarly for LCM. This associative property means you can compute the GCD or LCM of any number of integers by iterating pairwise. This calculator supports multiple inputs, computing both the pairwise Euclidean algorithm steps (for the first two numbers) and the prime factorization across all numbers. For the prime factorization method with multiple numbers, the GCD uses the minimum exponent across all factorizations, and the LCM uses the maximum exponent.

Frequently Asked Questions

What is the difference between GCD and LCM?
GCD (Greatest Common Divisor) is the largest number that divides all the given numbers evenly. LCM (Least Common Multiple) is the smallest number that is a multiple of all the given numbers. For example, GCD(12, 18) = 6 because 6 is the largest number that divides both. LCM(12, 18) = 36 because 36 is the smallest number divisible by both 12 and 18. They are inversely related: larger GCD means smaller LCM relative to the product.
What does it mean if the GCD is 1?
If GCD(a, b) = 1, the numbers are called coprime (or relatively prime). They share no common factor other than 1. This does not mean either number is prime. For example, 8 and 15 are coprime (GCD = 1) even though neither is prime. Coprime numbers have the property that LCM(a, b) = a * b. Coprimality is important in number theory, particularly in Euler's totient function and RSA cryptography.
Why is the Euclidean algorithm so efficient?
The Euclidean algorithm is efficient because the remainder decreases by at least half every two steps (by a theorem related to Fibonacci numbers). This gives it O(log(min(a,b))) time complexity, making it practical even for numbers with hundreds of digits. In contrast, trial division to find GCD would be O(sqrt(min(a,b))), which is exponentially slower for large numbers. The algorithm requires only integer division, making it fast on all hardware.
How do I find GCD of more than two numbers?
Use the property that GCD is associative: GCD(a, b, c) = GCD(GCD(a, b), c). Compute the GCD of the first two numbers, then compute the GCD of that result with the third number, and so on. The same approach works for LCM. This calculator handles multiple inputs automatically using this iterative approach.
What is the relationship between GCD, LCM, and fractions?
To simplify a fraction, divide both numerator and denominator by their GCD. To add or subtract fractions with different denominators, find the LCM of the denominators (least common denominator), convert both fractions, then add. For example, to add 1/4 + 1/6, LCM(4,6) = 12, so 3/12 + 2/12 = 5/12. This is why GCD and LCM are taught early in mathematics education.
Can GCD or LCM be negative?
By convention, GCD and LCM are always positive. If negative numbers are given as input, we use their absolute values. Some authors define GCD to always be non-negative, and it equals zero only when all inputs are zero. GCD(0, n) = |n| for any integer n, because every integer divides zero. LCM involving zero is typically defined as zero.

Was this tool helpful?