Reference · Math

math.lcm

signature
math.lcm(int | list...) -> int

What 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))
output
12 60 1291 12

Edge cases

print(math.lcm(0, 5), math.lcm(7), math.lcm(), math.lcm([]), math.lcm(-4, -6))print(math.lcm(range(1, 21)))
output
0 7 1 1 12232792560
this one fails
print(math.lcm(range(1, 50)))
output
line 1: integer overflow

Errors

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.

this one fails
print(math.lcm(4, "6"))
output
line 1: `math.lcm` expects an int as argument 2, got str

Chaining

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)
output
601212

See also

math.gcd, math.prod, math.factorial

Thread
7Useful? Like it, or add to its thread below.

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