Python Program to Find LCM of Two Numbers
Last Updated :
02 Jul, 2024
We are given two numbers and our task is to find the LCM of two numbers in Python. In this article, we'll discuss different approaches to finding the LCM of two numbers in Python.
Example:
Input: a = 12, b = 15
Output: 60
Explanation: LCM of 12 and 15 is 60
Python Program to Find LCM of Two Numbers
Below are some of the ways by which we can find the LCM of two numbers in Python:
Find the LCM of Two Numbers Using Loop
In this example, the function LCM(a, b)
calculates the Least Common Multiple (LCM) of two numbers a
and b
by iterating through multiples of the greater number within a range up to their product, and returns the first multiple that is divisible by the smaller number.
Python
def LCM(a, b):
greater = max(a, b)
smallest = min(a, b)
for i in range(greater, a*b+1, greater):
if i % smallest == 0:
return i
# Driver program to test above function
if __name__ == '__main__':
a = 12
b = 15
print("LCM of", a, "and", b, "is", LCM(a, b))
OutputLCM of 12 and 15 is 60
Find LCM of Two Numbers Using the GCD (Greatest Common Divisor)
In this example, the function lcm_using_gcd(a, b)
utilizes the property that the LCM of two numbers a
and b
is equal to their product divided by their greatest common divisor (GCD), calculated using math.gcd()
. It then returns the computed LCM.
Python
import math
def lcm_using_gcd(a, b):
gcd = math.gcd(a, b)
lcm = (a * b) // gcd
return lcm
# Example usage:
num1 = 12
num2 = 15
print("LCM of", num1, "and", num2, "is:", lcm_using_gcd(num1, num2))
OutputLCM of 12 and 15 is: 60
Find LCM of Two Numbers Using Prime Factorization
In this example, the prime_factors(n)
function generates the prime factors of a given number n
, while lcm_using_prime_factors(a, b)
computes the Least Common Multiple (LCM) of two numbers a
and b
using their prime factorizations. It combines the prime factors of both numbers, taking the maximum occurrences of each prime factor, and calculates their product to determine the LCM.
Python
def prime_factors(n):
factors = []
divisor = 2
while n > 1:
while n % divisor == 0:
factors.append(divisor)
n //= divisor
divisor += 1
return factors
def lcm_using_prime_factors(a, b):
factors_a = prime_factors(a)
factors_b = prime_factors(b)
lcm = 1
for factor in set(factors_a + factors_b):
lcm *= factor ** max(factors_a.count(factor), factors_b.count(factor))
return lcm
# Example usage:
num1 = 12
num2 = 15
print("LCM of", num1, "and", num2, "is:", lcm_using_prime_factors(num1, num2))
OutputLCM of 12 and 15 is: 60
Similar Reads
Python Program to Find the Gcd of Two Numbers The task of finding the GCD (Greatest Common Divisor) of two numbers in Python involves determining the largest number that divides both input values without leaving a remainder. For example, if a = 60 and b = 48, the GCD is 12, as 12 is the largest number that divides both 60 and 48 evenly. Using e
2 min read
Minimum of two numbers in Python In this article, we will explore various methods to find minimum of two numbers in Python. The simplest way to find minimum of two numbers in Python is by using built-in min() function. Pythona = 7 b = 3 print(min(a, b))Output3 Explanation:min() function compares the two numbers a and b.So, it retur
2 min read
Python Program to find the Quotient and Remainder of two numbers Given two numbers n and m. The task is to find the quotient and remainder of two numbers by dividing n by m. Examples: Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 4 Method 1: Naive approach The naive approach is to find the quotient using the
2 min read
Python Program to Find Numbers Divisible by Another Number We are given a list of numbers and a number. We have to find all the numbers in the list that are divisible by the given single number. Examples:Input: list=[8, 14, 21, 36, 43], num=3Output: 21, 36, 57Input: list=[2, 17, 25, 31, 48, 55], num=5Output: 25, 55In this article, we will discuss the differ
3 min read
Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range Given a range of numbers, the task is to write a Python program to find numbers divisible by 7 and multiple of 5. Example: Input:Enter minimum 100 Enter maximum 200 Output: 105 is divisible by 7 and 5. 140 is divisible by 7 and 5. 175 is divisible by 7 and 5. Input:Input:Enter minimum 29 Enter maxim
5 min read