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.
Computed via GMP's mpz_lcm folded across the arguments; sign is
normalised non-negative. Accepts BigInt and Rational inputs.
Examples¶
No verified examples yet for this function.
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.
Implementation status¶
Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
References¶
- Knuth, "The Art of Computer Programming, Vol. 2: Seminumerical Algorithms", on the Euclidean algorithm and least common multiples.
- von zur Gathen & Gerhard, "Modern Computer Algebra", on GCD/LCM relations.
- Source:
src/numbertheory.c - Specification:
docs/spec/builtins/number-theory.md
Notes & additional examples¶
Worked examples¶
The absorbing convention LCM[0, n] = 0:
LCM extends to rationals via numerator/denominator (lcm(numerators)/gcd(denominators)):
Folded across a range it gives the smallest number divisible by every integer 1..20, and large inputs promote to a GMP bigint:
In[1]:= LCM @@ Range[1, 20]
Out[1]= 232792560
In[2]:= LCM[123456789, 987654321]
Out[2]= 13548070123626141
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.