How to calculate Least Common Multiple in python
To find the least common multiple (L.C.M.) of two integers in the Python language, use the following formula
(a*b)/gcd(a,b)
- Parameters a and b are two integer numeric values.
- The function gcd calculates the greatest common divisor of the two numbers.
The formula calculates the least common multiple of the two numbers, i.e. the smallest common multiple of both numbers considered.
Note. The gcd () function is located in the Math library. Therefore, to use it, you must have imported it into the interpreter or script using the from import or import statement.
Examples
Example 1
The following script calculates the least common multiple (L.C.M.) of the numbers 14 and 6.
from math import gcd
(14*6)/gcd(14,6)
The formula returns as output
42.0
The integer number 42 is the least common multiple of 14 and 6.
Example 2
This script calculates the least common multiple (L.C.M.) of 12 and 6.
from math import gcd
(12*6)/gcd(12,6)
The function returns in output
12.0
The integer number 12 is the least common multiple of the numbers 12 and 6.