Answers for "How to find least common multiple of two numbers in Python"

1

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
Posted by: Guest on April-11-2022

Code answers related to "How to find least common multiple of two numbers in Python"

Python Answers by Framework

Browse Popular Code Answers by Language