2

Here is my code:

def lcm(a, b):
    if b == 0:
        return a
    return a * b / lcm(a, b)

print lcm(5,3)

This is what I could manage so far, any idea on how to find the LCM (least common multiple) of two numbers using recursive and one function?

alksdjg
  • 1,019
  • 2
  • 10
  • 26
ComputerGuy22
  • 41
  • 1
  • 2
  • 7

6 Answers6

2

We have lcm(a, b) * gcd(a, b) = a * b. So we can write the following equation:

lcm(a, b) = a; if a % b == 0
lcm(a, b) ; if a % b != 0
= a * b / gcd(a, b)
= a * b / gcd(b, a % b)
= a * b / (b * (a % b) / lcm(b, a % b))
= a / (a % b) * lcm(b, a % b)

And translate to Python, we have:

def lcm(a, b):
  t = a % b
  if t == 0: return a
  return a * lcm(b, t) / t
tsh
  • 4,263
  • 5
  • 28
  • 47
1

Edit: I didn't read the recursive / one function bit in your question cause I'm dumb. Incorporated now.


The lcm isn't a * b / lcm(a, b), it's a * b / gcd(a, b) (greatest common divisor).

So the cleanest way to do this is:

def gcd(x, y):
    while y:      
        x, y = y, x % y
    return x

def lcm(x, y):
    return x * y / gcd(x, y)

If you are limited to recursion only (e.g. for an exam) then this doesn't have to be efficient, so you might as well just recursively count up until you find the lowest number that both x and y divide into:

def lcm(x, y, counter=1):
    if (counter%x == 0 and counter%y == 0):
        return counter
    return lcm(x, y, counter+1)

That just increases counter until counter%x == 0 and counter%y == 0 is true, which is the LCM. Don't try it on large numbers though, you'll just get a stack overflow.

alksdjg
  • 1,019
  • 2
  • 10
  • 26
1

As stated in the other answers here lcm = a*b / gcd(a, b)but then you will need to define another function gcd(a, b) for it.

Since you needed only 1 function with recursion, maybe this piece of code will do.

N.B. : This function has one extra parameter c which should be always passed as 1 while calling it outside the function only :

def lcm(a, b, c):
d = c
m = min(a, b)
while m > 1 :
    if a%m == 0 and b%m == 0 :
        d*=m 
        return lcm(int(a/m), int(b/m), d)
    else:
        m-= 1
d*= a*b
return d
Arnav Das
  • 301
  • 2
  • 5
  • 11
1

Using the mathematical relationship that the product of two numbers is equal to the product of the Greatest Common Divisor and the Least Common Multiplier of those two numbers: A * B = GCD(A,B) * LCM(A,B)

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

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

The first function is recursive and it's used to find the Greatest Common Divisor, it has cost O(log(n)).

  • Please note that in the second function I'm using the integer division (useful when inputs are very large numbers) in order to avoid wrong answer due to the conversion between binary and floats! – Saverio Guzzo Jul 11 '19 at 15:43
0

This should do:

# Python Program to find the L.C.M. of two input number

# define a function
def lcm(x, y):
   """This function takes two
   integers and returns the L.C.M."""

   # choose the greater number
   if x > y:
       greater = x
   else:
       greater = y

   while True:
       if((greater % x == 0) and (greater % y == 0)):
           lcm = greater
           break
       greater += 1

   return lcm


# take input from the user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("The L.C.M. of", num1,"and", num2,"is", lcm(num1, num2))
Patrick Bassut
  • 3,310
  • 5
  • 31
  • 54
0

I created my own easy programme.

def lcm(greater,a,b):
# while(True):
    if (greater % a == 0 and greater % b == 0):
        lcm1 = greater
        return lcm1
    else:

        lcm1=lcm(greater + 1,a,b)
        return lcm1
a=int(input(" Enter 1st number :"))
b=int(input(" Enter 2nd number :"))
if(a>b):
  greater=a
else:
  greater=b

print(lcm(greater,a,b))
sameer_nubia
  • 721
  • 8
  • 8