GCD & LCM Calculator
Calculate greatest common divisor and least common multiple.Runs in this tab.⌘ ↵ runs it
Next step
Turn a click-through into an interactive demo
AtriumKit records your web app once in Chrome and turns every click into a step of a demo you can embed or share. There’s a free plan.
Try AtriumKit freeAbout GCD & LCM Calculator
The Euclidean algorithm as this calculator runs it
The greatest common divisor calculator uses the remainder form of Euclid’s algorithm. The whole loop is one line:
while (b) { [a, b] = [b, a % b]; } // when b hits 0, a is the GCD
Tracing it on Euclid’s own example, 1071 and 462:
| Step | a | b | a mod b |
|---|---|---|---|
| 1 | 1071 | 462 | 147 |
| 2 | 462 | 147 | 21 |
| 3 | 147 | 21 | 0 |
| done | 21 | 0 |
GCD(1071, 462) = 21. Input order doesn’t matter: if you enter the smaller number first, step 1 computes 462 mod 1071 = 462, which just swaps the pair, and the trace continues as above.
The loop is fast because the remainder shrinks quickly. The worst case is a pair of consecutive Fibonacci numbers, where every quotient is 1: 89 and 55 take 9 divisions (89 → 55 → 34 → 21 → 13 → 8 → 5 → 3 → 2 → 1). Lamé’s theorem caps the count at five times the number of digits in the smaller number, so even 15-digit inputs finish in at most 75 steps. You can generate test pairs with the Fibonacci generator.
More than two numbers
The calculator folds left across the list: GCD(a, b, c) = GCD(GCD(a, b), c), and LCM the same way, using LCM(a, b) = a × b / GCD(a, b) at each step.
84, 126, 210
GCD: gcd(84, 126) = 42 → gcd(42, 210) = 42
LCM: lcm(84, 126) = 252 → lcm(252, 210) = 1260
Don’t confuse “GCD of the set is 1” with “the numbers are pairwise coprime”. For 6, 10 and 15, the GCD is 1, but every pair shares a factor, so the LCM is 30, not the product 900.
Where developers actually use GCD and LCM
Reducing aspect ratios. Divide width and height by their GCD. This also shows when a marketing ratio is an approximation:
| Resolution | GCD | Exact ratio | Usually sold as |
|---|---|---|---|
| 1920 × 1080 | 120 | 16:9 | 16:9 |
| 3840 × 2160 | 240 | 16:9 | 16:9 |
| 2560 × 1080 | 40 | 64:27 | 21:9 |
| 1366 × 768 | 2 | 683:384 | 16:9 |
The aspect ratio calculator does this reduction for a width and height, and can lock the ratio while you resize.
Resampling audio. GCD(44100, 48000) = 300, so converting 44.1 kHz to 48 kHz is an exact rational ratio of 160/147: a polyphase resampler upsamples by 160 and downsamples by 147 (for example scipy.signal.resample_poly(x, 160, 147)).
Shared timebases. LCM(24, 30) = 120, which is why a 120 Hz display can show both 24 fps film and 30 fps video with whole-number frame repeats (5 and 4 refreshes per frame). LCM(24, 25, 30) = 600, the classic QuickTime timescale: one tick is 1/600 s, and frame durations at all three rates come out as whole numbers of ticks.
Scheduling. Jobs that run every 6, 10 and 15 minutes from the same start all fire together every LCM = 30 minutes. That’s the interval at which their load spikes stack.
How the input box parses numbers
Values are split on commas and whitespace, and each piece is read as an integer. Only positive integers are kept, and at least two must remain. The “Numbers:” line under the results shows exactly what was used, so check it when a result looks wrong:
| You type | Numbers used | Why |
|---|---|---|
12, 18 24 |
12, 18, 24 | Commas and spaces both separate |
1920x1080 |
none (error) | x isn’t a separator, so only 1920 is read |
1,000, 250 |
1, 250 | The thousands comma splits 1,000, and 000 (zero) is dropped |
-12, 18, 24 |
18, 24 | Zero and negative numbers are ignored |
12.5, 18 |
12, 18 | Decimals are truncated to integers |
Mathematically, zero and negatives are fine (GCD ignores sign, and GCD(a, 0) = a), so Python’s math.gcd(-12, 18) returns 6. This calculator simply leaves them out.
Precision limits, and exact one-liners
The calculator uses JavaScript numbers, which are exact only up to 2^53 − 1 (9,007,199,254,740,991), and it multiplies a × b before dividing by the GCD. Once that product passes the limit, rounding creeps into the LCM: LCM(53282097, 861866127144) displays as 14,651,724,161,447.998 instead of 14,651,724,161,448, and LCM(100000007, 100000037) shows 10,000,004,400,000,260 instead of …259. GCD results stay exact for any inputs below the limit, since the remainders only shrink.
For exact results on big numbers, use arbitrary-precision integers:
import math
math.gcd(84, 126, 210) # 42 (multiple arguments: Python 3.9+)
math.lcm(100000007, 100000037) # 10000004400000259
const gcd = (a, b) => b ? gcd(b, a % b) : a;
const lcm = (a, b) => a / gcd(a, b) * b; // divide first to keep values small
[100000007n, 100000037n].reduce(lcm); // 10000004400000259n
Questions
What is the Euclidean algorithm?
How are GCD and LCM related?
Can I calculate GCD for more than two numbers?
What does it mean if the GCD of two numbers is 1?
Does the GCD and LCM calculator work offline?
Comments
More in Math & Calculators
- Percentage Calculator
- Aspect Ratio Calculator
- Bitwise Calculator
- Scientific Calculator
- Hex Calculator
- Binary Calculator
- Matrix Calculator
- Prime Number Checker
Comments