Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 983 Bytes

20221213094557.md

File metadata and controls

35 lines (30 loc) · 983 Bytes

Least common multiple

I learned about the "least common multiple" the other day which is the least possible number that is divisible by all the numbers in a list of numbers.

For example, the LCM of 2, 3, and 4 == 12.

Python has a function in the math module to conveniently calculate this for you:

>>> import math
>>> numbers = [2, 3, 4]
>>> math.lcm(*numbers)
12
>>> for i in range(1, 14):
...     print(i, " => ", end="")
...     for n in numbers:
...         print(f"{n}: {i % n == 0}", end=" ")
...     print()
...
1  => 2: False 3: False 4: False
2  => 2: True 3: False 4: False
3  => 2: False 3: True 4: False
4  => 2: True 3: False 4: True
5  => 2: False 3: False 4: False
6  => 2: True 3: True 4: False
7  => 2: False 3: False 4: False
8  => 2: True 3: False 4: True
9  => 2: False 3: True 4: False
10  => 2: True 3: False 4: False
11  => 2: False 3: False 4: False
12  => 2: True 3: True 4: True  # <= lcm 12 for 2,3,4
13  => 2: False 3: False 4: False

#math