GCD LCM calculator
Enter 12 and 18 and get a GCD of 6 and an LCM of 36, or pass up to 20 numbers in one go. Handy for reducing fractions, finding common denominators and lining up repeating cycles. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Numbers
- 12, 18
- Greatest common divisor
- 6
- Least common multiple
- 36
Computes the greatest common divisor and least common multiple of an array of 2 to 20 integers, returning the normalized inputs alongside the gcd and lcm.
How the GCD and LCM are computed
The greatest common divisor of two values comes from the Euclidean algorithm — repeatedly replacing (a, b) with (b, a mod b) until b is zero — and the whole-array gcd is that operation applied left to right across every element. The least common multiple of a pair is computed as abs((a / g) * b) where g is their gcd, dividing before multiplying to keep the intermediate value small, and the array lcm is likewise folded across all elements.
If any element is zero the lcm is zero, and an all-zero array is rejected because gcd(0,0) is undefined.
Normalization, limits and safe integers
Each element is first coerced with Math.trunc and Math.abs, so signs and fractional parts are discarded: -12.9 becomes 12 before any math runs, and the normalized values are what appear in the returned numbers array. The array must hold between 2 and 20 elements, every element must be a finite number, and no absolute value may exceed 1e15.
Deterministic arithmetic you can reuse
GCD and LCM underpin everyday tasks: reducing fractions to lowest terms, finding a common denominator, aligning repeating schedules or tiling dimensions, and simplifying ratios. Passing more than two numbers is fully supported — the fold handles up to 20 in one call — so you can reduce an entire set at once instead of chaining pairwise requests.
What you can do with it
Fraction and ratio reduction
Reduce fractions or ratios to lowest terms by dividing numerator and denominator by their gcd.
Scheduling alignment
Find when repeating cycles coincide by taking the lcm of their periods across up to 20 values.
FAQ
What happens to negatives and decimals?
Each value is truncated and made absolute (Math.trunc then Math.abs), so -12.9 becomes 12 before any computation.
How many numbers can I enter at once?
Between 2 and 20. The GCD and LCM are worked out across the whole list in a single pass, so you can reduce an entire set instead of chaining pairs by hand.
What happens if one of the numbers is zero?
The LCM becomes zero, since zero is a multiple of every number. A list made only of zeros is rejected, because the greatest common divisor of zeros is not defined.
How do I use the LCM to see when two schedules line up?
Enter the length of each repeating cycle, for example in days. The LCM is the first point where all of them coincide again, and it works for up to 20 cycles at once.
How do I reduce a fraction with the GCD?
Enter the top and bottom numbers of the fraction and divide both by the GCD you get. The result is the same fraction in lowest terms, and the same trick simplifies a ratio.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/dev/gcd-lcm \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"numbers":[12,18]}'const res = await fetch("https://api.kit.forhosting.com/dev/gcd-lcm", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"numbers": [
12,
18
]
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/dev/gcd-lcm",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"numbers": [
12,
18
]
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/dev/gcd-lcm", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"numbers":[12,18]}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"numbers":[12,18]}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/dev/gcd-lcm", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"numbers": [
12,
18
]
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "dev.gcd_lcm",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Limits
max_items | 20 |
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
How the GCD and LCM are computed
dev.gcd_lcm takes a numbers array and folds it pairwise. The response returns numbers (the normalized inputs), gcd and lcm, so the example [12, 18] yields a gcd of 6 and an lcm of 36.
Normalization, limits and safe integers
Because LCM grows fast, the result is checked against Number.MAX_SAFE_INTEGER and a computed lcm beyond that range returns a typed invalid_input error rather than a silently wrong float. Any violation — a non-array, too few or too many items, a non-finite entry, an out-of-range magnitude, or an all-zero set — raises invalid_input with a message naming the problem, before or during the fold, so callers get a clean, catchable failure.
Integration use cases
Batch number theory
Call the API over many integer sets to append gcd and lcm columns without a local math library.
Technical questions
How many numbers can I pass?
Between 2 and 20 integers per call; the gcd and lcm are folded pairwise across the whole array.
Are there size limits?
No element may exceed 1e15 in magnitude, and an LCM beyond MAX_SAFE_INTEGER returns invalid_input instead of a wrong float.
What if I pass all zeros?
It is rejected: gcd(0,0) is undefined, so an all-zero array returns a typed invalid_input error.
Is it deterministic and free?
Yes. The same array always yields the same result; the browser widget is free and the API bills only successful tasks at $0.002.