-2

def multiple(a, b): """so I'm trying to return the smallest number n that is a multiple of both a and b.

for example:

multiple(3, 4) 12 multiple(14, 21) 42 """

def gcd (a,b):
    if a < b : a , b = b,a
    while b:
        a , b = b , a % b
    return a

def lcm (a , b):
    n= (a*b) / gcd(a,b)
    return n

it keeps throwing errors about indentation and logic. I don't understand why. I've tried changing the variables around too.

urviguglani
  • 127
  • 1
  • 1
  • 11

1 Answers1

2

No need to find GCD, we can directly find LCM. Below code works

def lcmof(x,y):
    res=0
    mx=max(x,y)
    mn=min(x,y)
    for i in range(1,mx+1,1):
        temp=mx*i
        try:
            if(temp%mn==0):
                res=temp
                break
        except ZeroDivisionError:
            res=0
            break
    return res
Lokesh Sanapalli
  • 1,012
  • 3
  • 18
  • 39
  • 1
    For large values of x and y, this is much slower than the approach given in the question, which is of order log(min(x,y)). – Rory Daulton Sep 07 '16 at 12:10
  • Ok, this did not work for me, i put 32 and 16 and it should put out 2 ( aka 2**5 is 32 and 2**4 is 16 ) it gave me a 16 as final result. – Danilo Aug 07 '17 at 22:22