Functional Weave
Code in TypeScript

math.gcd-lcm

Greatest common divisor and least common multiple of two integers, exactly and without overflow.

1.0.0 (not the latest) · published 2026-10-03 by charlie · Anterra

Pinned by 15 tests, run in TypeScript, Python and Rust.

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.

For example

  • gcdLcm(12, 18) → gcd 6, lcm 36 12 and 18
  • gcdLcm(17, 5) → gcd 1, lcm 85 coprime numbers
  • gcdLcm(7, 7) → gcd 7, lcm 7 equal numbers

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

export function gcdLcm(a: number, b: number): GcdLcm
aintany integer within ±(2^53 - 1)
bintany integer within ±(2^53 - 1)
returnsGcdLcm

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;
}

Your code names it in one line, in the file that uses it

import { gcdLcm } from "#fune/math.gcd-lcm@^1";
impl/typescript.ts · 49 lines · open · raw
import { type GcdLcm } from "./math_gcd_lcm_types.ts";

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. Never negative; gcdWide(0, 0) is 0. */
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;
}

export function gcd(a: number, b: number): number {
  checkSafe("a", a);
  checkSafe("b", b);
  return Number(gcdWide(BigInt(a), BigInt(b)));
}

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);
}

/**
 * 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) };
}

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
Download for TypeScript math.gcd-lcm-1.0.0-typescript.fune · 6,157 bytes sha256 1a08c0eccf54258a34c79d011d0b3cf2491d638a644ef32c58b2029b1aa843bf

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./math.gcd-lcm-1.0.0-typescript.fune, or fetch it from a terminal with fune pull math.gcd-lcm@1.0.0:typescript.

The whole function, every language, is one file too: math.gcd-lcm-1.0.0.fune, 10,259 bytes, sha256 c9e41636d824c974f9a85f3a786451805447acfbe1af4ee8a0b3e2d4f96448b9. 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

after — your function gets the result and the arguments, and returns the final result.

// fune: after math.gcd-lcm

replace — it requires no other capability, so there is no dependency to replace.

step — your function runs at a numbered point inside the 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 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.

CaseArgumentsExpected
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
CaseArgumentsExpected
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

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.

Also exported, for capabilities that build on this one: `gcd(a, b)`, `lcm(a, b)`, and `gcdWide`, a gcd over wide integers (TypeScript `bigint`, Python `int`, Rust `i128`) with no range limit, used by `math.rational` to reduce intermediate products before they are checked.

Files

PathBytes
README.md1,073
impl/python.py1,484
impl/rust.rs2,429
impl/typescript.ts1,499
vectors.json1,587