Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

How to find least common multiple of two numbers in Python

"""
Absolute value of product of two numbers
is equal to product of GCD and LCM
gcd(a, b) * lcm(a, b) = |a*b|
=> lcm(a, b) = |a*b|/gcd(a, b)
"""
import math

def lcm(a, b):
    if a == 0 or b == 0:
        return 0
    else:
        gcd = math.gcd(a, b)
        return abs(a * b) / gcd

print(int(lcm(12, 18)))  # 36
 
PREVIOUS NEXT
Tagged: #How #find #common #multiple #numbers #Python
ADD COMMENT
Topic
Name
7+5 =