Built-in module to calculate the least common multiple

In Python 3.8 and earlier

There is no such thing built into the stdlib.

However, there is a Greatest Common Divisor function in the math library. (For Python 3.4 or 2.7, it’s buried in fractions instead.) And writing an LCM on top of a GCD is pretty trivial:

def lcm(a, b):
    return abs(a*b) // math.gcd(a, b)

Or, if you’re using NumPy, it’s come with an lcm function for quite some time now.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)