LCM of Two Numbers in Python | Find LCM in Python

LCM of Two Numbers in Python | Find LCM in Python

LCM of Two Numbers in Python

The Least Common Multiple (LCM) is a fundamental concept in mathematics that refers to the smallest positive integer that is divisible by two or more numbers. Whether you’re working on an algorithm, solving a math problem, or just curious about LCMs, Python offers multiple ways to compute the LCM of two numbers. In this article, we’ll explore various methods to find the LCM of two numbers using Python, accompanied by practical code examples.

What is LCM?

LCM of Two Numbers in PythonThe LCM of two numbers is the smallest positive integer that both numbers divide without leaving a remainder. For example:
  • The LCM of 12 and 24 is 24, because 24 is the smallest number that is divisible by both 12 and 24.
Understanding LCM is useful in many fields such as mathematics, coding, and problem-solving, especially when working with ratios, fractions, or synchronous cycles.

Input and Output Format

Let’s understand the input-output format for LCM computation in Python.

Input:

  • You will be given two integers.

Output:

  • The program will output the LCM of the given two numbers.

Sample Input:

plaintext
12 24

Sample Output:

plaintext
24

Algorithm to Find LCM of Two Numbers

Here’s a simple algorithm to compute the LCM of two numbers in Python:
  1. Input the two integers.
  2. Determine the larger number using an if condition.
  3. Use a while loop to check if the larger number is divisible by both numbers.
  4. If divisible, print the LCM and exit.
  5. If not, increase the larger number by 1 and check again.
  6. Repeat until the LCM is found.

Methods to Find the LCM of Two Numbers

Let’s explore different methods to compute the LCM in Python.

Method 1: Using a Simple Loop

This method involves using a simple loop to find the least common multiple by checking divisibility.
python
# Python Program to find the LCM of Two Numbers num1 = int(input()) # Input first number num2 = int(input()) # Input second numberif num1 > num2: max_num = num1 else: max_num = num2while True: if max_num % num1 == 0 and max_num % num2 == 0: print(max_num) break max_num += 1
Explanation:
  • This method checks the larger number and increments it until it finds a number divisible by both num1 and num2.
Sample Input:
plaintext
12 24
Sample Output:
plaintext
24

Method 2: Using Functions for Reusability

If you want to make the code reusable, you can define a function to calculate the LCM. This method is suitable for scenarios where you need to find the LCM for different pairs of numbers.
python
# Python Program to find LCM of Two Numbers using Functions def find_lcm(a, b): if a > b: maximum = a else: maximum = bwhile True: if maximum % a == 0 and maximum % b == 0: lcm = maximum break maximum += 1 return lcm# Taking inputs from the user num1 = int(input()) num2 = int(input()) lcm = find_lcm(num1, num2) # Function call print(lcm)
Explanation:
  • This function accepts two numbers and returns the LCM using the same logic as before but wrapped in a function for better reusability.
Sample Input:
plaintext
12 26
Sample Output:
plaintext
156

Method 3: Using GCD to Calculate LCM

A more efficient approach to find the LCM is using the Greatest Common Divisor (GCD). The formula for LCM using GCD is:LCM(a,b)=a×bGCD(a,b)\text{LCM}(a, b) = \frac{a \times b}{\text{GCD}(a, b)}This method is faster than brute-force checking.
python
# Python Program to find the LCM of Two Numbers using GCD num1 = int(input()) # Input first number num2 = int(input()) # Input second number# Find GCD using Euclidean algorithm a = num1 b = num2 while b != 0: temp = b b = a % b a = tempgcd = a lcm = (num1 * num2) // gcd # LCM using GCD print(lcm)
Explanation:
  • The program first calculates the GCD using the Euclidean algorithm and then calculates the LCM using the relationship between LCM and GCD.
Sample Input:
plaintext
3 8
Sample Output:
plaintext
24

Method 4: Using Recursion for LCM

A more advanced and compact way to find the LCM is by using recursion to calculate the GCD, and then apply the formula for LCM.
python
# Python Program to find the LCM of Two Numbers using Recursion def find_gcd(a, b): if b == 0: return a else: return find_gcd(b, a % b)num1 = int(input()) # Input first number num2 = int(input()) # Input second numbergcd = find_gcd(num1, num2) # Function call to find GCD lcm = (num1 * num2) // gcd # LCM using the formula print(lcm)
Explanation:
  • This method uses recursion to find the GCD and then calculates the LCM as before.
Sample Input:
plaintext
28 56
Sample Output:
plaintext
56

Real-World Applications of LCM

  1. Synchronization Problems: LCM helps in determining when two events will occur at the same time after starting at different intervals.
  2. Fraction Operations: When adding or subtracting fractions, the LCM of the denominators is used to find a common denominator.
  3. Computing Time Intervals: LCM is used to calculate recurring cycles, like schedules that repeat at different intervals.

Visuals to Complement This Section:

  1. Flowchart for LCM Calculation: A flowchart outlining the process of LCM calculation, from input to output.
  2. GCD and LCM Formula: A visual representation of how LCM and GCD are related, with a formula.
  3. Sample Code Output: Code snippets with highlighted output sections for better understanding.

Conclusion

Finding the LCM of two numbers in Python can be done in multiple ways, ranging from a simple loop to more optimized methods using GCD or recursion. Depending on your application, you can choose the method that best fits your needs. Whether you’re a beginner or an advanced programmer, mastering LCM calculation is a valuable skill in Python programming.Click Here to Know more!LCM of Two Numbers in Python 
c