LCM of Two Numbers in Python

LCM of two numbers in python | Least or lowest common multiple (LCM) of two integers a and b is the smallest positive number that is divisible by both a and b.

Example:-
LCM of 3 and 5 is 15 because 15 is divisible by both 3 and 5.
LCM of 12 and 15 is 60 because 60 is divisible by both 12 and 15.

Find LCM in Python

This is a normal method to find lcm of the two numbers in python. We will take two numbers while declaring the variables. Python program to find lcm of the two numbers using if-else statement and while loop.

# Python program to find the LCM of the two numbers

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# choose the greater number
if (num1 > num2):
    greater = num1
else:
    greater = num2

while(True):
    # find LCM
    if(greater % num1 == 0 and greater % num2 == 0):
        print('The LCM of',num1,'and',num2,'is',greater)
        break
    greater += 1

Output for the different input value:-

Enter first number: 2
Enter second number: 4
The LCM of 2 and 4 is 4

Enter first number: 3
Enter second number: 5
The LCM of 3 and 5 is 15

Enter first number: 20
Enter second number: 8
The LCM of 20 and 8 is 40

In each iteration, we check if both the numbers perfectly divide our number. If so, we store the number as L.C.M. and break from the loop. Otherwise, the number is incremented by 1 and the loop continues.

LCM Function in Python

We can also take the help of a function to find lcm of the two numbers in python. A function is a block of code that performs a specific task.

# Python program to find the LCM using function

def find_lcm(a, b):   #user-defined function
   # choose the greater number
   if a > b:
       greater = a
   else:
       greater = b

   while(True):
       # find LCM
       if((greater % a == 0) and (greater % b == 0)):
           lcm = greater
           break
       greater += 1
   return lcm

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# calling function & display result
print('The LCM of',num1,'and',num2,'is',find_lcm(num1, num2))

Output:-

Enter first number: 50
Enter second number: 40
The LCM of 50 and 40 is 200

Program using GCD

The above program methods are slower to run. We can make it more efficient and faster by using the fact that the product of two numbers a and b is equal to the product of HCF(a,b) and LCM(a,b).

a*b = HCF(a, b) * LCM(a, b)

The HCF ( highest common factor ) is also referred also as GCD ( Greatest Common Measure ), Using this formula we can find GCD and LCM at a time. We need to find either GCD and LCM and then apply this formula.

In the below program to find the LCM of two numbers in python; First, we find the HCF then using the formula LCM will be calculated. The Formula used for this purpose is:-

LCM(a, b) = (a*b) / HCF(a, b)

# Python program to find the LCM using GCD

# This function find GCD 
def find_gcd(a, b):
    while(b):
        a, b = b, a % b
    return a

# This function find LCM
def find_lcm(a, b):
    lcm = (a*b)//find_gcd(a,b)
    return lcm

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# calling function & display result
print('The LCM of',num1,'and',num2,'is',find_lcm(num1, num2))

Output:-

Enter first number: 10
Enter second number: 25
The LCM of 10 and 25 is 50

LCM of Two Numbers in Python using Recursion

We can also use the recursion technique to find the lcm of two numbers. A technique of defining the method/function that contains a call to itself is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can handle easily. This is also a well-known computer programming technique: divide and conquer.

# Python program to find the LCM using recursion

# This recursive function find GCD 
def find_gcd(a, b):
    if(b == 0):
        return a
    else:
        return find_gcd(b, a%b)

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# find LCM
lcm = (num1 * num2) // find_gcd(num1, num2)

# display result
print('The LCM of',num1,'and',num2,'is',lcm)

Output:-

Enter first number: 9
Enter second number: 31
The LCM of 9 and 31 is 279

Also See:- Find Factorial of a Number in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *