Priya R
Updated date Nov 16, 2023
In this blog, we will learn how to calculate the Least Common Multiple (LCM) using Python through various methods.

## Introduction:

The Least Common Multiple (LCM) is a fundamental concept in mathematics that finds its applications in various fields, such as number theory, algebra, and computer science. It plays a crucial role in solving problems related to fractions, proportions, and arithmetic operations. In this blog, we will explore different methods to calculate the LCM in Python,

## Method 1: Using Python's math library

The simple way to calculate the LCM in Python is by using the math library, which provides a built-in function called `lcm`. This method is both efficient and easy to use.

``````import math

num1 = 12
num2 = 18

lcm = math.lcm(num1, num2)

print("Method 1 - Using math library:")
print(f"LCM of {num1} and {num2} is {lcm}")
``````

### Output:

``````Method 1 - Using math library:
LCM of 12 and 18 is 36
``````

In this method, we import the math library and use the `math.lcm()` function to calculate the LCM of two numbers, `num1` and `num2`. It's a straightforward approach that's suitable for most LCM calculations.

## Method 2: Using the GCD (Greatest Common Divisor) method

The LCM of two numbers can also be calculated using their Greatest Common Divisor (GCD). This method relies on the relationship between LCM and GCD, which states that LCM(a, b) = (a * b) / GCD(a, b).

To implement this method in Python, you need to find the GCD of the two numbers and then use it to calculate the LCM.

``````def find_gcd(a, b):
while b:
a, b = b, a % b
return a

def calculate_lcm(a, b):
return (a * b) // find_gcd(a, b)

num1 = 12
num2 = 18

lcm = calculate_lcm(num1, num2)

print("Method 2 - Using GCD:")
print(f"LCM of {num1} and {num2} is {lcm}")
``````

### Output:

``````Method 2 - Using GCD:
LCM of 12 and 18 is 36
``````

In this method, we define two functions, `find_gcd` and `calculate_lcm`. The `find_gcd` function calculates the GCD of two numbers using the Euclidean algorithm. Then, the `calculate_lcm` function uses the GCD to compute the LCM by using the LCM formula. This method is particularly useful when you want to understand the underlying mathematics of LCM calculations.

## Method 3: Using a custom LCM function

Another approach is to create a custom LCM function without relying on the built-in `math` library. This method allows you to have more control and a deeper understanding of how LCM is calculated.

``````def find_lcm(a, b):
max_num = max(a, b)
while True:
if max_num % a == 0 and max_num % b == 0:
return max_num
max_num += 1

num1 = 12
num2 = 18

lcm = find_lcm(num1, num2)

print("Method 3 - Custom LCM function:")
print(f"LCM of {num1} and {num2} is {lcm}")
``````

### Output:

``````Method 3 - Custom LCM function:
LCM of 12 and 18 is 36
``````

In this method, we create a custom `find_lcm` function. It starts with the larger of the two numbers and iterates until it finds a number that is divisible by both `a` and `b`. This approach is less efficient than using the GCD method but provides a clear understanding of the LCM calculation process.

## Method 4: Using a list of numbers

To find the LCM of more than two numbers, you can use a list of numbers and iteratively calculate the LCM for each pair of numbers. This method is helpful when dealing with multiple values.

``````def find_lcm_of_list(numbers):
if len(numbers) < 2:
return None

lcm = numbers[0]
for num in numbers[1:]:
lcm = calculate_lcm(lcm, num)

return lcm

numbers = [12, 18, 24, 36]

lcm = find_lcm_of_list(numbers)

print("Method 4 - LCM of a list of numbers:")
print(f"LCM of {numbers} is {lcm}")
``````

### Output:

``````Method 4 - LCM of a list of numbers:
LCM of [12, 18, 24, 36] is 72
``````

In this method, we define a `find_lcm_of_list` function that takes a list of numbers as input. The function iterates through the list, calculating the LCM of each pair of numbers and updating the LCM value. This process continues until the LCM of all numbers in the list is found.

## Method 5: Using the Reduce function

Python's `functools` library provides the `reduce` function, which can be used to find the LCM of a list of numbers efficiently. This method is concise and Pythonic.

``````from functools import reduce

def calculate_lcm(a, b):
return (a * b) // find_gcd(a, b)

def find_lcm_of_list(numbers):
return reduce(calculate_lcm, numbers)

numbers = [12, 18, 24, 36]

lcm = find_lcm_of_list(numbers)

print("Method 5 - Using the Reduce function:")
print(f"LCM of {numbers} is {lcm}")
``````

### Output:

``````Method 5 - Using the Reduce function:
LCM of [12, 18, 24, 36] is 72
``````

In this method, we define the `calculate_lcm` and `find_lcm_of_list` functions, similar to the previous methods. The `reduce` function from the `functools` library is used to apply the `calculate_lcm` function iteratively to the list of numbers. This method is efficient and concise, making it a great choice for finding the LCM of a list of numbers.

## Conclusion:

Calculating the Least Common Multiple (LCM) is an essential mathematical operation with practical applications in various domains. In this blog, we have explored five different methods to calculate the LCM in Python:

• Using Python's math library, which provides a straightforward solution.
• Using the GCD method, leveraging the relationship between LCM and GCD.
• Creating a custom LCM function for a deeper understanding of the process.
• Calculating the LCM of a list of numbers by iteratively finding the LCM of pairs.
• Utilizing the `reduce` function from the `functools` library for efficient LCM calculations on a list of numbers.