Skip to content

LCM

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

LCM[n1, n2, ...]

gives the least common multiple of the integers ni, the smallest positive integer they all divide; with the gcd it satisfies GCD[a, b] LCM[a, b] == a b.

Notes Computed via GMP's mpz\_lcm folded across the arguments; sign is normalised non-negative. Accepts BigInt and Rational inputs.

Examples (6)

Every input below was run against the current Mathilda build and its output recorded.

Applications (6)

In[1]:= LCM[3, 4, 5]
Out[1]= 60

In[2]:= LCM[12, 18, 30]
Out[2]= 180

In[3]:= LCM[0, 5]
Out[3]= 0

In[4]:= LCM[1/2, 2/3, 3/4]
Out[4]= 6

In[5]:= LCM @@ Range[1, 20]
Out[5]= 232792560

In[6]:= LCM[123456789, 987654321]
Out[6]= 13548070123626141

Implementation notes

Algorithm. builtin_lcm mirrors builtin_gcd. It folds pairwise with lcm(a,b)=ab/gcd(a,b): an int64 fast path, a GMP path (mpz_lcm) when any argument is a EXPR_BIGINT, and a rational fold using lcm(a/b, c/d) = lcm(a,c)/gcd(b,d) (numerator accumulated with mpz_lcm, denominator with mpz_gcd). A zero argument zeroes the running LCM (and short-circuits). LCM[] is 1, LCM[x] is |x|; non-rational arguments return NULL.

Data structures. GMP mpz_t accumulators; expr_bigint_normalize demotes results that fit in int64, and mpz_pair_to_rational_expr reduces the rational result. Shares the rational num/den coercion helpers with GCD.

Attributes: Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected.

References

See also: GCD

Notes & additional examples

Notes

LCM uses the identity lcm(a, b) = a*b / gcd(a, b) and folds across all arguments, so LCM[3, 4, 5] gives 60 and LCM[12, 18, 30] gives 180. The absorbing convention LCM[0, n] = 0 holds, matching the fact that zero is the only common multiple involving zero. Pairwise reduction keeps intermediate values small, and results promote to GMP bigints when they exceed machine-word range.