Python math.lcm() - Least Common Multiple
Python math.lcm()
math.lcm(*integers) function returns the Least Common Multiple (LCM) of the integers passed as arguments.
Syntax
The syntax to call lcm() function is
math.lcm(*integers)
where
Parameter | Required | Description |
---|---|---|
*integers | No | None, one or multiple integer values. |
If no integer value is passed as argument to lcm(), then the return value is 1.
If only one integer is passed to lcm(), then then same value is returned as LCM.
If any non-integral value is passed as argument to lcm(), then the function raises TypeError.
Examples
1. LCM of two integers
In the following program, we find the LCM of two integers: 10 and 25.
Python Program
import math
result = math.lcm(10, 25)
print('lcm() :', result)
Output
lcm() : 50
2. LCM of one integer
In the following program, we find the LCM of a single integer value: 5.
Python Program
import math
result = math.lcm(5)
print('lcm() :', result)
Output
lcm() : 5
3. LCM of float values
In the following program, we try to find the LCM of floating point values. math.lcm() throws TypeError, because the method accepts only integer values.
Python Program
import math
result = math.lcm(5.5)
print('lcm() :', result)
Output
TypeError: 'float' object cannot be interpreted as an integer
Summary
In this Python Math tutorial, we learned the syntax of, and examples for math.lcm() function.