Python Program - Find LCM of Two Numbers
LCM stands for Least Common Multiple. The LCM of two numbers is the smallest number that can be divided by both numbers.
For example - LCM of 20 and 25 is 100 and LCM of 30 and 40 is 120.
Mathematically, LCM of two numbers (a and b) can be expressed as below:
a x b = LCM(a, b) x GCD(a, b) LCM(a, b) = (a x b) / GCD(a, b)
Method 1: Using For Loop to find GCD and LCM of two numbers
In the example below, for loop is used to iterate the variable i from 1 to the smaller number. If both numbers are divisible by i, then it modifies the GCD and finally gives the GCD of two numbers. GCD of two numbers is then used to calculate LCM of two numbers.
x = 20 y = 25 if x > y: x, y = y, x for i in range(1,x+1): if x%i == 0 and y%i == 0: gcd = i lcm = (x*y)/gcd print("LCM of", x, "and", y, "is:", lcm)
The above code will give the following output:
LCM of 20 and 25 is: 100.0
Method 2: Using While Loop to find GCD and LCM of two numbers
In the example below, larger number is replaced by a number which is calculated by subtracting the smaller number from the larger number. The process is continued until the two numbers become equal which will be GCD of two numbers. GCD of two numbers is then used to calculate LCM of two numbers.
p = x = 20 q = y = 25 while x != y: if x > y: x = x - y else: y = y - x lcm = (p*q)/x print("LCM of", p, "and", q, "is:", lcm)
The above code will give the following output:
LCM of 20 and 25 is: 100.0
Method 3: Using the recursive function to find GCD and LCM of two numbers
In the example below, recursive function is used which uses Euclidean algorithm to find GCD of two numbers which is further used to calculate LCM of two numbers.
def gcd(x, y): if y == 0: return x return gcd(y, x%y) x = 30 y = 40 lcm = (x*y)/gcd(x,y) print("LCM of", x, "and", y, "is:", lcm)
The above code will give the following output:
LCM of 30 and 40 is: 120.0
Method 4: Using gcd() function of math module
The LCM of two numbers can be calculated using gcd() function of math module. Consider the following example.
import math as ma x = 80 y = 100 lcm = (x*y)/ma.gcd(x,y) print("LCM of", x, "and", y, "is:", lcm)
The above code will give the following output:
LCM of 80 and 100 is: 400.0
Method 5: Using lcm() function of math module
Please note that, the lcm() function is added in version 3.9.
import math as ma x = 80 y = 100 lcm = ma.lcm(x, y) print("LCM of", x, "and", y, "is:", lcm)
The above code will give the following output:
LCM of 80 and 100 is: 400.0
Recommended Pages
- Python Program - To Check Prime Number
- Python Program - Bubble Sort
- Python Program - Selection Sort
- Python Program - Maximum Subarray Sum
- Python Program - Reverse digits of a given Integer
- Python - Swap two numbers
- Python Program - Fibonacci Sequence
- Python Program - Insertion Sort
- Python Program - Find Factorial of a Number
- Python Program - Find HCF of Two Numbers
- Python Program - To Check Whether a Number is Palindrome or Not
- Python Program - To Check Whether a String is Palindrome or Not
- Python Program - Heap Sort
- Python Program - Quick Sort
- Python - Swap Two Numbers without using Temporary Variable
- Python Program - To Check Armstrong Number
- Python Program - Counting Sort
- Python Program - Radix Sort
- Python Program - Find Largest Number among Three Numbers
- Python Program - Print Floyd's Triangle