The math
module in Python contains a number of mathematical operations.
Amongst some of the most important functions in this module is the lcm()
function which returns the least common multiple of the specified integer arguments.
The
lcm
function was newly introduced in the Python version 3.9.0.
The Least Common Multiple (LCM) of a group of numbers is the least possible number that is divisible by all the numbers in the group.
For example, the LCM of 2, 3, and 4 is 12, because 12 is the least number which is divisible by all three numbers.
The syntax of the math.lcm()
function is shown below.
math.lcm(x1, x2, x3,... xn)
Note: The
math.lcm()
function is available in Python 3.9.0 and higher.
The math.lcm()
function accepts n integers, where n = any real number.
The math.lcm()
function returns the least common multiple of the specified numbers.
- If all the provided numbers are , it returns .
- If no arguments are provided, it returns .
- If a
float
orstring
parameter is provided, themath.lcm()
method returns aTypeError
.
The code snippet below shows how the math.lcm()
works in Python.
import mathprint(math.lcm(2,3,4))
Line 1 imports the math
module.
In line 2, the math.lcm()
method computes the LCM of 2, 3, and 4, which are printed accordingly.