Reference · Math
math.lcm
math.lcm(int | list...) -> intWhat it does
The least common multiple of some ints, as Python's math.lcm: the smallest positive
int that each of them divides. The ints come as separate arguments or as one list;
math.lcm(4, 6) and math.lcm([4, 6]) are both 12. The answer is never negative.
Examples
print(math.lcm(4, 6), math.lcm(4, 6, 10), math.lcm([4, 6]))print(math.lcm(7, 13), math.lcm(-4, 6))12 60 1291 12Edge cases
- Any zero makes the answer
0:math.lcm(0, 5)is0. - One int gives its absolute value, and no ints at all (or an empty list) give
1, as in Python. - Negative ints count by their absolute value.
- The multiple of many ints grows fast: past what an int holds (about
9.2 × 10¹⁸) it is a runtime error,
integer overflow, not a float.
print(math.lcm(0, 5), math.lcm(7), math.lcm(), math.lcm([]), math.lcm(-4, -6))print(math.lcm(range(1, 21)))0 7 1 1 12232792560print(math.lcm(range(1, 50)))line 1: integer overflowErrors
An argument that is not an int, or a list item that is not, is a runtime error, not an error value. A list may only be the whole argument list.
print(math.lcm(4, "6"))line 1: `math.lcm` expects an int as argument 2, got strChaining
A list in a pipeline is the only argument, so xs | math.lcm reduces the whole list;
a number in a pipeline is the first of two. The lowest common denominator of some
fractions:
print([4, 6, 10] | math.lcm)print(4 | math.lcm(6))let denominators = [2, 3, 4]print(denominators | math.lcm)601212See also
math.gcd, math.prod, math.factorial
Thread 0 posts
On math.lcm(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo