math.gcd-lcm
Greatest common divisor and least common multiple of two integers, exactly and without overflow.
2.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 39 tests, run in TypeScript, Python and Rust.gcdLcm 15 · gcd 12 · lcm 12
What it does
Returns the greatest common divisor (also called the highest common factor) and the least common multiple of two integers. Both are never negative: `gcd(-4, 6)` is 2 and `lcm(-4, 6)` is 12.
The conventions at zero are the ones every maths library uses: `gcd(0, n)` is `|n|`, `gcd(0, 0)` is 0, and the lcm of anything with 0 is 0.
The functions
A group: 3 functions that work together, each in its own file, each pinned by its own tests in TypeScript, Python and Rust. A project can install only the ones it calls.
The type it declares, generated into your project
/** Both answers together; each is never negative. */
export interface GcdLcm {
/** greatest common divisor; gcd(0, 0) is 0 */
readonly gcd: number;
/** least common multiple; 0 when either input is 0 */
readonly lcm: number;
}
Once installed, your code imports each one from the group's module.
gcdLcm throws on bad input 15 tests
export function gcdLcm(a: number, b: number): GcdLcm
| a | int | any integer within ±(2^53 - 1) |
| b | int | any integer within ±(2^53 - 1) |
| returns | GcdLcm |
For example
gcdLcm(12, 18)→ gcd 6, lcm 36 12 and 18gcdLcm(17, 5)→ gcd 1, lcm 85 coprime numbersgcdLcm(7, 7)→ gcd 7, lcm 7 equal numbers
import { gcdLcm } from "#fune/math.gcd-lcm@^2";
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { gcd } from "./math_gcd_lcm_gcd.ts"; ← gcd, another function of this group · built into the same file, even by a slim install
import { lcm } from "./math_gcd_lcm_lcm.ts"; ← lcm, another function of this group · built into the same file, even by a slim install
import { type GcdLcm } from "./math_gcd_lcm_types.ts";
/**
* Greatest common divisor and least common multiple of two integers.
*
* Both are returned together because a caller needing one nearly always
* needs the other (common denominators, repeating schedules).
*/
export function gcdLcm(a: number, b: number): GcdLcm {
return { gcd: gcd(a, b), lcm: lcm(a, b) };
}gcd throws on bad input 12 tests
export function gcd(a: number, b: number): number
| a | int | any integer within ±(2^53 - 1) |
| b | int | any integer within ±(2^53 - 1) |
| returns | int | never negative; gcd(0, 0) is 0 |
For example
gcd(48, 180)→ 12 48 and 180 share 12gcd(35, 64)→ 1 coprime numbers share only 1gcd(180, 48)→ 12 order does not matter
import { gcd } from "#fune/math.gcd-lcm@^2";
function checkSafe(name: string, value: number): void {
if (!Number.isInteger(value)) {
throw new TypeError(`${name} must be an integer, received ${value}`);
}
if (!Number.isSafeInteger(value)) {
throw new RangeError(`${name} is outside the safe integer range (±9007199254740991)`);
}
}
/**
* Euclid's algorithm on wide integers, with no range limit. Never negative;
* gcdWide(0, 0) is 0. Exported for fraction arithmetic (math.rational), which
* reduces products that are already past 2^53 before it checks them.
*/
export function gcdWide(a: bigint, b: bigint): bigint {
let x = a < 0n ? -a : a;
let y = b < 0n ? -b : b;
while (y !== 0n) {
const r = x % y;
x = y;
y = r;
}
return x;
}
/** Greatest common divisor, never negative; gcd(0, n) is |n| and gcd(0, 0) is 0. */
export function gcd(a: number, b: number): number {
checkSafe("a", a);
checkSafe("b", b);
return Number(gcdWide(BigInt(a), BigInt(b)));
}lcm throws on bad input 12 tests
export function lcm(a: number, b: number): number
| a | int | any integer within ±(2^53 - 1) |
| b | int | any integer within ±(2^53 - 1) |
| returns | int | never negative; 0 when either input is 0; an error beyond 2^53 - 1 |
For example
lcm(4, 6)→ 12 4 and 6 meet at 12lcm(6, 42)→ 42 one divides the otherlcm(9, 28)→ 252 coprime numbers multiply
import { lcm } from "#fune/math.gcd-lcm@^2";
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { gcd } from "./math_gcd_lcm_gcd.ts"; ← gcd, another function of this group · built into the same file, even by a slim install
/** Least common multiple, never negative; 0 when either input is 0. */
export function lcm(a: number, b: number): number {
const g = gcd(a, b);
if (g === 0) return 0;
// Divide before multiplying: a * b overflows long before the lcm does.
const result = (BigInt(Math.abs(a)) / BigInt(g)) * BigInt(Math.abs(b));
if (result > BigInt(Number.MAX_SAFE_INTEGER)) {
throw new RangeError(`the lcm of ${a} and ${b} exceeds 2^53 - 1`);
}
return Number(result);
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add math.gcd-lcm
That builds the whole group. To build only what you call, and whatever it uses inside the group:
fune add math.gcd-lcm --only gcdLcm
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./math.gcd-lcm-2.0.0-typescript.fune, or fetch it from a terminal with fune pull math.gcd-lcm@2.0.0:typescript.
The whole function, every language, is one file too: math.gcd-lcm-2.0.0.fune, 19,234 bytes, sha256 b4ff66bde30ed40b4e53d980014fb8902c3854539d276a87c0c083ba74f7a1bb. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
// fune: before math.gcd-lcm.gcdLcm
// fune: before math.gcd-lcm.gcd
// fune: before math.gcd-lcm.lcm
after — your function gets the result and the arguments, and returns the final result.
// fune: after math.gcd-lcm.gcdLcm
// fune: after math.gcd-lcm.gcd
// fune: after math.gcd-lcm.lcm
replace — it requires no other capability, so there is no dependency to replace.
step — your function runs at a numbered point inside a function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show math.gcd-lcm --steps.
// fune: step math.gcd-lcm.<fn> after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
gcdLcm 15 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| 12 and 18 | 12, 18 | → | gcd 6, lcm 36 |
| coprime numbers | 17, 5 | → | gcd 1, lcm 85 |
| equal numbers | 7, 7 | → | gcd 7, lcm 7 |
| one divides the other | 4, 20 | → | gcd 4, lcm 20 |
| one with anything | 1, 999 | → | gcd 1, lcm 999 |
| negatives give non-negative answers | -4, 6 | → | gcd 2, lcm 12 |
| both negative | -12, -18 | → | gcd 6, lcm 36 |
| zero and n: gcd is n, lcm is zero | 0, 5 | → | gcd 5, lcm 0 |
| zero and a negative | -9, 0 | → | gcd 9, lcm 0 |
| zero and zero | 0, 0 | → | gcd 0, lcm 0 |
Show the other 5 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| a times b overflows even though the lcm fits: divide first | 3,000,000,021, 5,000,000,035 | → | gcd 1,000,000,007, lcm 15,000,000,105 |
| the largest safe integer with itself | 9,007,199,254,740,991, 9,007,199,254,740,991 | → | gcd 9,007,199,254,740,991, lcm 9,007,199,254,740,991 |
| an lcm beyond 2^53 - 1 is an error | 9,007,199,254,740,991, 9,007,199,254,740,990 | → | error: exceeds 2^53 - 1 |
| an input beyond 2^53 - 1 is an error | 9,007,199,254,740,992, 2 | → | error: outside the safe integer range |
| a fractional input is an error | 1.5, 3 | → | error: must be an integer |
gcd 12 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| 48 and 180 share 12 | 48, 180 | → | 12 |
| coprime numbers share only 1 | 35, 64 | → | 1 |
| order does not matter | 180, 48 | → | 12 |
| a negative input gives a positive divisor | -48, 180 | → | 12 |
| both negative | -21, -14 | → | 7 |
| gcd of zero and n is |n| | 0, -17 | → | 17 |
| gcd of zero and zero is zero | 0, 0 | → | 0 |
| consecutive Fibonacci numbers, Euclid's slowest case, are coprime | 5,527,939,700,884,757, 8,944,394,323,791,464 | → | 1 |
| the largest safe integer and one less share only 1 | 9,007,199,254,740,991, 9,007,199,254,740,990 | → | 1 |
| the largest safe integer with a factor of it | 9,007,199,254,740,991, 6,361 | → | 6,361 |
Show the other 2 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| an input beyond 2^53 - 1 is an error | 2, 9,007,199,254,740,992 | → | error: outside the safe integer range |
| a fractional input is an error | 4, 2.5 | → | error: must be an integer |
lcm 12 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| 4 and 6 meet at 12 | 4, 6 | → | 12 |
| one divides the other | 6, 42 | → | 42 |
| coprime numbers multiply | 9, 28 | → | 252 |
| a negative input gives a positive multiple | -4, 6 | → | 12 |
| both negative | -15, -20 | → | 60 |
| anything with zero is zero | 0, 12 | → | 0 |
| zero with zero is zero | 0, 0 | → | 0 |
| a times b would overflow, but dividing first fits | 3,000,000,021, 5,000,000,035 | → | 15,000,000,105 |
| an lcm of exactly 2^53 - 1 | 9,007,199,254,740,991, 1 | → | 9,007,199,254,740,991 |
| an lcm of 3 x 2^52 is past the limit | 4,503,599,627,370,496, 3 | → | error: exceeds 2^53 - 1 |
Show the other 2 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| an input beyond 2^53 - 1 is an error | -9,007,199,254,740,992, 3 | → | error: outside the safe integer range |
| a fractional input is an error | 0.5, 4 | → | error: must be an integer |
More from the author
The lcm is computed as `|a| / gcd * |b|`, dividing first. The textbook `a * b / gcd` overflows a 64-bit integer, and loses digits in a JavaScript number, long before the answer itself is large; one of the vectors is a case where it does.
Inputs and results are limited to ±(2^53 - 1), the range where a JavaScript number is still an exact integer, so the three languages agree on every answer. An input outside it, or an lcm beyond it, is an error.
This is a group of three functions: `gcdLcm(a, b)` returns both answers, `gcd(a, b)` and `lcm(a, b)` one each. `lcm` uses `gcd`, and `gcdLcm` uses both, so `require math.gcd-lcm ^2.0.0 only=gcd` installs the gcd alone.
`gcdWide(a, b)`, a gcd over wide integers (TypeScript `bigint`, Python `int`, Rust `i128`) with no range limit, is still exported from the `gcd` file for fraction arithmetic that reduces products already past 2^53. It is a helper, not a published function: the registry's type vocabulary has no wide integer (`int` is a JavaScript number and an `i64`), so it has no `fn` line and no vectors of its own, and it is exercised through `gcd`'s vectors. Its signature and behaviour are exactly those of 1.0.0.
## What changed from 1.0.0
1.0.0 was one function, `gcdLcm`, with `gcd`, `lcm` and `gcdWide` exported alongside it but unpinned: no signature in the manifest and no vectors, so nothing held the three languages to the same answer for them. 2.0.0 is a group: `gcd` and `lcm` are published functions with their own signatures, files and vectors, and `gcdLcm` keeps every 1.0.0 vector.
No answer changed. The Rust adapters now refuse a fractional argument with the same "must be an integer" wording as TypeScript and Python, which only affects the vectors. It is a new major version because the package's shape and public surface changed: it installs as one module per function plus the group module (`math_gcd_lcm` still re-exports every function and `gcdWide`), a project can take only some of it, and the Python module no longer exports its `MAX_SAFE` constant. Dependents stay on `^1.0.0` until they move deliberately; 1.0.0 is unchanged.
Files
| Path | Bytes |
|---|---|
| README.md | 2,466 |
| impl/python/gcd.py | 1,048 |
| impl/python/gcd_lcm.py | 417 |
| impl/python/lcm.py | 452 |
| impl/rust/gcd.rs | 1,537 |
| impl/rust/gcd_lcm.rs | 1,153 |
| impl/rust/lcm.rs | 1,061 |
| impl/typescript/gcd.ts | 961 |
| impl/typescript/gcd_lcm.ts | 465 |
| impl/typescript/lcm.ts | 518 |
| vectors.json | 4,274 |