GCD / HCF Calculator
Calculate the greatest common divisor (GCD) or highest common factor (HCF) of two or more numbers with step-by-step solutions
Updated
What is the GCD / HCF Calculator?
Find the Greatest Common Divisor (GCD) — also known as the Highest Common Factor (HCF) — of two or more numbers instantly. This calculator goes beyond simple answers: it shows you every step of the Euclidean algorithm, reveals prime factorizations, calculates the LCM, and even finds Bézout coefficients for number theory enthusiasts.
What is GCD / HCF?
The Greatest Common Divisor (GCD) or Highest Common Factor (HCF) of two or more integers is the largest positive integer that divides each of them without leaving a remainder.
- GCD(48, 18) = 6 because 6 is the largest number that divides both 48 and 18
- GCD(17, 19) = 1 — these numbers are coprime (share no common factors except 1)
Why Use This Calculator?
| Feature | Benefit |
|---|---|
| Step-by-Step Work | See the Euclidean algorithm in action |
| Multiple Numbers | Calculate GCD for 2, 3, 4+ numbers at once |
| Prime Factorization | Understand the building blocks of your numbers |
| LCM Included | Get the Least Common Multiple automatically |
| Bézout Identity | Discover the linear combination that equals your GCD |
| Fraction Simplification | See how GCD reduces fractions to lowest terms |
Perfect For
- Students learning number theory and divisibility
- Teachers demonstrating the Euclidean algorithm
- Engineers working with gear ratios and periodic systems
- Programmers implementing cryptographic algorithms
- Anyone who needs to simplify fractions or find common denominators
Enter your numbers above — separated by commas, spaces, or new lines — and explore the mathematics behind GCD.
How it works
This calculator uses classical number theory algorithms to compute results with complete mathematical transparency. Here is exactly what happens behind the scenes.
1. Input Parsing
Your input is split into individual numbers using flexible delimiters:
- Commas:
48, 18, 24 - Spaces:
48 18 24 - New lines:
481824 - Mixed:
48, 18 24
Each token is validated to ensure it is a finite integer. Decimals and non-numeric values are rejected with clear error messages.
2. The Euclidean Algorithm (Core GCD Calculation)
For two numbers a and b, the algorithm repeatedly applies:
gcd(a, b) = gcd(b, a mod b)
until the remainder is zero. The last non-zero remainder is the GCD.
Example: GCD(48, 18)
| Step | Equation | Remainder |
|---|---|---|
| 1 | 48 = 18 × 2 + 12 | 12 |
| 2 | 18 = 12 × 1 + 6 | 6 |
| 3 | 12 = 6 × 2 + 0 | 0 |
Result: GCD = 6
Multiple Numbers
For more than two numbers, the calculator applies the associative property:
gcd(a, b, c) = gcd(gcd(a, b), c)
This runs the algorithm sequentially, using each result as input to the next pair.
3. Prime Factorization
Each number is decomposed using trial division:
- Divide by 2 until odd
- Test odd divisors up to √n
- Remaining value > 1 is prime
Example: 56 = 2³ × 7
This reveals why the GCD works: the GCD takes the minimum exponent for each common prime.
4. LCM Calculation
The Least Common Multiple uses:
lcm(a, b) = |a × b| / gcd(a, b)
Using the table method with quotient tracking:
| Step | r (remainder) | s | t | q (quotient) |
|---|---|---|---|---|
| 0 | 48 | 1 | 0 | — |
| 1 | 18 | 0 | 1 | 2 |
| 2 | 12 | 1 | -2 | 1 |
| 3 | 6 | -1 | 3 | 2 |
| 4 | 0 | — | — | — |
Result: 48 × (-1) + 18 × 3 = 6
6. Fraction Simplification
For number pairs, the calculator shows:
originalNumerator / originalDenominator ÷ gcd = simplified
Example: 48/18 ÷ 6 = 8/3
Key Mathematical Properties
| Property | Description |
|---|---|
| Non-negativity | GCD is always ≥ 0, even with negative inputs |
| gcd(0, 0) undefined | All zeros rejected with error |
| gcd(a, 0) = |a| | Zero handled correctly |
| Associativity | gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) |
| Commutativity | gcd(a, b) = gcd(b, a) |
Performance Notes
- Time complexity: O(log min(a, b)) for two numbers
- Space complexity: O(1) for iterative implementation
- JavaScript limits: Numbers beyond 2⁵³ may lose precision; arbitrary precision not implemented
Examples
GCD of two numbers
Find the greatest common divisor of 48 and 18 using the Euclidean algorithm
GCD of three numbers
Calculate GCD for multiple numbers with prime factorization
Coprime numbers
Check if numbers are coprime (GCD equals 1)
Negative numbers
GCD works with negative integers (uses absolute values)
Frequently asked questions
What is the difference between GCD and HCF?
What is the difference between GCD and HCF?
GCD (Greatest Common Divisor) and HCF (Highest Common Factor) are identical concepts with different names. Both refer to the largest positive integer that divides two or more numbers without a remainder. "GCD" is more common in American mathematics and computer science, while "HCF" is frequently used in British and Indian educational systems. This calculator returns the same value regardless of which term you prefer.
Can I calculate GCD for more than two numbers?
Can I calculate GCD for more than two numbers?
Yes. Enter three, four, or more numbers separated by commas, spaces, or new lines. The calculator uses the associative property of GCD: gcd(a, b, c) = gcd(gcd(a, b), c). It computes the GCD of the first two numbers, then uses that result with the third number, and continues until all numbers are processed. The steps panel shows the progression through each pairwise calculation.
Why does the calculator show negative numbers in Bézout coefficients?
Why does the calculator show negative numbers in Bézout coefficients?
The Bézout identity finds integers x and y (which may be negative) such that ax + by = gcd(a, b). Negative coefficients are mathematically correct and expected. For example, gcd(48, 18) = 6 is expressed as 48 × (-1) + 18 × 3 = 6. The calculator automatically adjusts signs to match your original input values (including their signs).
What happens if I enter zero?
What happens if I enter zero?
The calculator handles single zeros correctly: gcd(a, 0) = |a| and lcm(a, 0) = 0. However, all zeros (0, 0, 0) produces an error because the GCD of zero and zero is mathematically undefined (every integer divides zero). At least one non-zero number is required for a meaningful result.
How do I use this to simplify fractions?
How do I use this to simplify fractions?
Enter your numerator and denominator as two numbers. The calculator displays a "Simplified Fractions" section showing the original fraction, the GCD used, and the reduced result. For example, entering 48, 18 shows 48/18 simplifies to 8/3 by dividing both by 6. This works for any pair of integers, including negative fractions.
What does "coprime" mean?
What does "coprime" mean?
Two or more numbers are coprime (or relatively prime) if their GCD equals 1. This means they share no prime factors. For example, 17 and 19 are coprime (both prime, different values), as are 8 and 15 (2³ and 3 × 5 share nothing). The calculator displays "Coprime? Yes" when this occurs. Note: numbers need not be prime themselves to be coprime to each other.
Can I see the prime factors of my numbers?
Can I see the prime factors of my numbers?
Yes. Enable "Show factorization" to see the prime decomposition of every input number. Each number is displayed as a product of primes with exponents, such as 56 = 2³ × 7. This helps visualize why the GCD is what it is: the GCD takes the lowest power of each common prime across all numbers.
What is the relationship between GCD and LCM?
What is the relationship between GCD and LCM?
For two numbers, GCD × LCM = |a × b|. The GCD captures shared factors; the LCM captures all factors at their highest occurrence. When you enter numbers, both values are calculated automatically. For multiple numbers, the relationship generalizes: gcd(a, b, c) × lcm(a, b, c) does not equal a × b × c, but each can be computed from prime exponent minima and maxima respectively.
Why is my result different from another calculator?
Why is my result different from another calculator?
Possible reasons: (1) The other calculator uses absolute values while yours included sign handling — both should give the same magnitude. (2) Integer overflow: numbers above 9,007,199,254,740,991 (2⁵³) may lose precision in JavaScript. (3) Different algorithms: some calculators use binary GCD (Stein's algorithm) which is faster but produces identical results. This calculator uses the classical Euclidean algorithm for educational transparency.
How do I copy or share my results?
How do I copy or share my results?
Click "Copy GCD" or "Copy LCM" for single values, or "Copy All Results" for a complete text summary including prime factorizations and steps. The formatted text includes all calculated values and can be pasted into documents, emails, or homework submissions. No account or login is required.
Does this work with negative numbers?
Does this work with negative numbers?
Yes. The GCD is defined for negative integers and equals the GCD of their absolute values. Enter -48, 18 and the calculator returns 6. The result is always non-negative by mathematical convention. The Bézout coefficients adjust their signs to accommodate your original negative inputs.
What is the Extended Euclidean Algorithm?
What is the Extended Euclidean Algorithm?
The Extended Euclidean Algorithm computes GCD and finds Bézout coefficients simultaneously. While the standard algorithm only gives the remainder chain, the extended version tracks additional variables to express the GCD as a linear combination of the inputs. This is essential in cryptography (RSA key generation) and solving linear Diophantine equations. Enable "Show Bézout identity" to see this in action.
Related tools
More utilities you might find handy.
Circle Calculator
Calculate circle radius, diameter, circumference, and area from any one known value. Instant results with adjustable precision.
Cube Root Calculator
Calculate cube root of any number
Decimal & Base Converter
Instantly convert numbers between decimal, binary, hexadecimal, octal, and any custom base from 2 to 36.