Find 3 of the lowest common multiples of 3 numbers

Viewed 34
def mult_comun(x, y, z):
    mult_co = []
    mult = mult_com(x, y, z)
        for i in range(1, 4):
            mult *= i
            mult_co.append(mult)
    return mult_co
print(mult_comun(a, b, c))

This is the code I wrote, but I'm not sure it's working right (I don't think that the math in this case works like this. I mean, multiplying the least common multiple by 2, 3 and 4)

(this is the mult_com() function I defined earlier, used for finding the least common multiple)

def mult_com(x, y, z):
    mult = (x * y* z) // (div_com(x, y, z) ** 2)
    return mult
print("Cel mai mic multiplu comun este ", mult_com(a, b, c))
2 Answers

Ok, I found the math.lcm() function, so the mult_com() is edited now into

def mult_com(x, y, z):
    mult = math.lcm(math.lcm(x, y), z)
    return mult

Again, I am not saying that the mult_comun() is not working, it's just that I am not sure that that is the best / correct way of finding what I'm looking for.

There is a neat trick to finding the least common multiple (LCM). In mathematics,

LCM * HCF = Product(x, y)

We can use this idea and find the LCM.

# Function to find HCF the Using the Euclidian algorithm is as follows
def compute_hcf(x, y):
   while(y):
       x, y = y, x % y
   return x

Now, taking the product of numbers and dividing it by its HCF would give you the LCM;

def compute_lcm(a, b):
    return a * b / compute_hcf(a, b)
Related