GCD and LCM Calculator
Calculate the GCD (GCF/HCF) and LCM of two or more numbers instantly. See step-by-step Euclidean algorithm and prime factorization methods with accurate results.
GCD and LCM Calculator
| Step | Euclidean Modulo Division | Remainder |
|---|---|---|
| 1 | 48 = (2 × 18) + 12 | 12 |
| 2 | 18 = (1 × 12) + 6 | 6 |
| 3 | 12 = (2 × 6) + 0 | 0 |
| Number | Prime Factor Exponent Form |
|---|---|
| 48 | 2^4 × 3 |
| 18 | 2 × 3^2 |
1. Introduction to GCD, LCM, GCF, and HCF
In computational number theory and arithmetic, the Greatest Common Divisor (GCD)—also universally known as the Greatest Common Factor (GCF) or Highest Common Factor (HCF)—and the Least Common Multiple (LCM) represent fundamental integer properties.
The ToolMono GCD & LCM Calculator computes both values simultaneously for 2, 3, 4, or more integers using 100% client-side BigInt precision and dynamic Euclidean algorithm step equations. Explore companion tools: Prime Number Checker, Online Calculator, Scientific Calculator, Binary ↔ Decimal Converter, Decimal ↔ Hex Converter, and Decimal ↔ Octal Converter.
2. How to Use the Calculator
Type 2 or more integers separated by commas or spaces (e.g. 12, 18, 24, 36).
Choose Euclidean Algorithm, Prime Factorization, Listing Factors, or Listing Multiples.
View live GCD, LCM, simplified ratio (e.g. 8:3), and dynamic Euclidean modulo equations.
Click Copy GCD, Copy LCM, or Copy Steps to export formatted outputs for homework or code.
3. What Is GCD? What Is LCM?
The Greatest Common Divisor of two or more non-zero integers is the largest positive integer that divides each input without a remainder. For example, GCD(12, 18) = 6 because 6 divides both 12 (12 ÷ 6 = 2) and 18 (18 ÷ 6 = 3).
The Least Common Multiple is the smallest positive integer that is divisible by each input in the set. For example, LCM(12, 18) = 36 because 36 is the smallest integer common to the multiples of 12 (12, 24, 36...) and 18 (18, 36...).
4. Euclidean Algorithm Method
Documented by Euclid in 300 BCE, the Euclidean Algorithm relies on the fundamental identity:gcd(a, b) = gcd(b, a mod b)
It repeatedly computes remainder r = a mod b and replaces (a, b) with (b, r) until r = 0. The last non-zero remainder is the GCD.
By Lamé's Theorem, the Euclidean algorithm runs in O(log(min(|a|, |b|))) steps, taking less than a microsecond even for huge BigInt integers.
5. Prime Factorization Method
Decomposing integers into prime factors allows calculating both GCD and LCM using exponent min/max rules:
6. Listing Factors and Multiples Method
For students and beginners, listing divisors and multiples directly is the most intuitive method:
7. Method Comparison Table
| Calculation Method | Primary Strengths | Best Used For |
|---|---|---|
| Euclidean Algorithm | Fastest logarithmic performance O(log N), zero memory overhead | Large BigInt integers and high-speed software code |
| Prime Factorization | Computes both GCD and LCM simultaneously via exponent matrices | Understanding integer structure and number theory homework |
| Listing Factors / Multiples | Visual listing of divisors and multiples | Beginners, primary education, and quick mental math |
8. How to Find GCD and LCM of Multiple Numbers
GCD and LCM satisfy associative laws, allowing multi-number sets to be evaluated iteratively:
9. GCD vs. GCF vs. HCF
Depending on regional curriculum standards, different acronyms are used to describe the exact same mathematical value:
| Acronym | Full Form | Regional / Academic Standard |
|---|---|---|
| GCD | Greatest Common Divisor | Computer Science, University Mathematics, Software Engineering |
| GCF | Greatest Common Factor | United States & Canadian K-12 School Curricula |
| HCF | Highest Common Factor | United Kingdom, India, Australia, & Commonwealth Curricula |
10. Relationship Between GCD and LCM
For two non-zero integers a and b, their product equals the product of their GCD and LCM:GCD(a, b) × LCM(a, b) = |a × b|
Therefore, LCM can be calculated directly once GCD is known:LCM(a, b) = (|a| / GCD(a, b)) × |b|
The simple relationship GCD × LCM = product applies ONLY to two numbers. For 3 or more numbers, GCD(a, b, c) × LCM(a, b, c) ≠ a × b × c in general! Always evaluate multi-number sets iteratively.
11. GCD and LCM Edge Cases
| Input Pair | GCD | LCM | Notes & Mathematical Conventions |
|---|---|---|---|
| 12, 18 | 6 | 36 | Standard composite case |
| 7, 11 | 1 | 77 | Coprime prime numbers (GCD = 1) |
| 12, 12 | 12 | 12 | Equal numbers (GCD = LCM = |a|) |
| 12, 0 | 12 | 0 | Zero handling (GCD = |a|, LCM = 0) |
| 0, 0 | Undefined | 0 | GCD of (0,0) is undefined under standard math definitions |
| -12, 18 | 6 | 36 | Uses absolute values (|a|, |b|) |
12. Worked Examples
13. Production Code Implementations
/**
* High-Precision JavaScript (ES6+) GCD & LCM Module using BigInt
*/
// Iterative Euclidean Algorithm for BigInt
export function gcdBigInt(a, b) {
a = a < 0n ? -a : a;
b = b < 0n ? -b : b;
while (b !== 0n) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
// LCM calculated with overflow prevention: (a / gcd) * b
export function lcmBigInt(a, b) {
if (a === 0n || b === 0n) return 0n;
const g = gcdBigInt(a, b);
return (a / g) * b;
}
// Array reduction for multiple numbers
export function gcdMultiple(numbers) {
return numbers.map(BigInt).reduce((acc, val) => gcdBigInt(acc, val));
}
export function lcmMultiple(numbers) {
return numbers.map(BigInt).reduce((acc, val) => lcmBigInt(acc, val));
}/**
* Strongly-Typed TypeScript GCD/LCM Calculator Suite
*/
export type NumericInput = number | string | bigint;
export class NumberTheoryEngine {
public static gcd(a: NumericInput, b: NumericInput): bigint {
let n1 = BigInt(a);
let n2 = BigInt(b);
n1 = n1 < 0n ? -n1 : n1;
n2 = n2 < 0n ? -n2 : n2;
while (n2 !== 0n) {
const remainder = n1 % n2;
n1 = n2;
n2 = remainder;
}
return n1;
}
public static lcm(a: NumericInput, b: NumericInput): bigint {
const n1 = BigInt(a);
const n2 = BigInt(b);
if (n1 === 0n || n2 === 0n) return 0n;
const commonDivisor = this.gcd(n1, n2);
return (n1 / commonDivisor) * n2;
}
public static gcdArray(values: NumericInput[]): bigint {
if (values.length === 0) return 0n;
return values.map((v) => BigInt(v)).reduce((acc, curr) => this.gcd(acc, curr));
}
public static lcmArray(values: NumericInput[]): bigint {
if (values.length === 0) return 0n;
return values.map((v) => BigInt(v)).reduce((acc, curr) => this.lcm(acc, curr));
}
}import math
from typing import List, Tuple
def compute_gcd(a: int, b: int) -> int:
"""Computes Greatest Common Divisor using Python math module."""
return math.gcd(a, b)
def compute_lcm(a: int, b: int) -> int:
"""Computes Least Common Multiple using safe division."""
if hasattr(math, 'lcm'):
return math.lcm(a, b)
g = math.gcd(a, b)
return 0 if g == 0 else (abs(a) // g) * abs(b)
def compute_list_gcd_lcm(numbers: List[int]) -> Tuple[int, int]:
"""Computes GCD and LCM across a list of integers."""
g = math.gcd(*numbers)
l = numbers[0]
for n in numbers[1:]:
l = (l * n) // math.gcd(l, n)
return g, l14. Common Mathematical & Coding Errors
- Confusing Factors and Multiples: Remember that GCD(a, b) ≤ min(|a|, |b|) while LCM(a, b) ≥ max(|a|, |b|).
- 64-bit Integer Overflow in LCM: Computing
(a * b) / gcdcan overflow integer limits. Always compute(a / gcd) * binstead. - Neglecting Absolute Values: Passing negative inputs without applying absolute values can produce negative outputs or infinite loops in modulo code.
15. Performance & Complexity Analysis
- Time Complexity: O(log(min(|a|, |b|))) operations. Execution takes less than 1 millisecond even for 100-digit integers.
- Space Complexity: O(1) auxiliary memory for iterative Euclidean modulo.
- Client-Side Speed: Uses native BigInt in your local browser V8 memory without remote API calls.
16. Mathematical & Engineering Best Practices
Use (a / gcd) * b in software code to prevent fixed-width numeric overflow.
Convert inputs via Math.abs() as GCD and LCM are positive by definition.
17. Frequently Asked Questions
Related Tools
Browse all toolsPrime Number Checker
Check if a number is prime or composite instantly. Get factors, prime factorization, trial-division details, and neighboring prime numbers with this free online prime checker.
Online Calculator
Use ToolMono's free online calculator for quick everyday math, expressions, percentages, powers, roots, and parentheses. Includes calculation history and keyboard support.
Scientific Calculator
Use ToolMono's free Scientific Calculator for advanced mathematical calculations. Supports trigonometric, inverse, hyperbolic, logarithmic, exponential, factorial, scientific notation, degree/radian modes, calculation history, and 100% client-side processing.
Binary ↔ Decimal Converter
Free online Binary Decimal Converter. Convert binary base-2 to decimal base-10 instantly. Supports BigInt, two's complement, 8–256 bit signed, and step breakdown.
Decimal ↔ Hex Converter
Free online Decimal to Hex Converter. Convert base-10 to base-16 hexadecimal instantly. Supports BigInt, two's complement, 8–64 bit signed/unsigned, and live steps.
Decimal to Octal Converter
Free online Decimal to Octal converter with instant step-by-step division steps. Convert Base-10 to Base-8 and Octal to Decimal accurately in your browser.