What is the math.lcm() function in Python?

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.

Solution approach

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.

Syntax

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.

Parameters

The math.lcm() function accepts n integers, where n = any real number.

Return value

The math.lcm() function returns the least common multiple of the specified numbers.

  • If all the provided numbers are 00, it returns 00.
  • If no arguments are provided, it returns 11.
  • If a float or string parameter is provided, the math.lcm() method returns a TypeError.

Code

The code snippet below shows how the math.lcm() works in Python.

import math
print(math.lcm(2,3,4))

Explanation

  • 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.