Lcm and hcf using python

Hey fellow coder! Today in this tutorial, we will learn how to compute the highest common factor (HCF) and Lowest common multiplier (LCM) using the python programming language.

Let us first understand what do we mean by HCF and LCM of two numbers if you are not familiar with these terms as of now.

Also read: Calculating Precision in Python — Classification Error Metric


What is Highest Common Factor (HCF)?

The highest common factor of two numbers is defined as the greatest common factor of the two numbers. For example, let’s consider two numbers 12 and 18.

The two numbers mentioned have the common factors as 2,3, and 6. The highest out of the three is 6. So in this case the HCF is 6.


What is Lowest Common Multiplier (LCM)?

The smallest/lowest common multiple of the two numbers is called the lowest common multiplier of the two numbers. For example, let’s consider the two numbers 12 and 18 again.

The multipliers of the two numbers can be 36, 72, 108, and so on. But we need the lowest common multipliers so the LCM of 12 and 18 will be 36.


Calculate HCF and LCM in Python

Let’s get right into implementing HCF and LCM in Python code.

1. Finding HCF of two numbers

a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))

HCF = 1

for i in range(2,a+1):
    if(a%i==0 and b%i==0):
        HCF = i

print("First Number is: ",a)
print("Second Number is: ",b)
print("HCF of the numbers is: ",HCF)

Let us pass two numbers as input and see what our results come out to be.

First Number is:  12
Second Number is:  18
HCF of the numbers is:  6

2. Finding LCM of two numbers

After we have computed the HCF of the two numbers, finding the LCM is not a tough task. LCM is simply equal to the product of the number divided by the HCF of the numbers.

a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))

HCF = 1

for i in range(2,a+1):
    if(a%i==0 and b%i==0):
        HCF = i

print("First Number is: ",a)
print("Second Number is: ",b)

LCM = int((a*b)/(HCF))
print("LCM of the two numbers is: ",LCM)

Let us pass the two numbers and see what the results turn out to be.

First Number is:  12
Second Number is:  18
LCM of the two numbers is:  36


Conclusion

I hope you are now clear with the computation of HCF and LCM of two numbers. And I guess you have also learned about the implementation of the same in the python programming language.

Thank you for reading! Happy learning! 😇


In this program, you'll learn to find the LCM of two numbers and display it.

To understand this example, you should have the knowledge of the following Python programming topics:

  • Python while Loop
  • Python Functions
  • Python Function Arguments
  • Python User-defined Functions

The least common multiple (L.C.M.) of two numbers is the smallest positive integer that is perfectly divisible by the two given numbers.

For example, the L.C.M. of 12 and 14 is 84.

Program to Compute LCM

# Python Program to find the L.C.M. of two input number

def compute_lcm(x, y):

   # choose the greater number
   if x > y:
       greater = x
   else:
       greater = y

   while(True):
       if((greater % x == 0) and (greater % y == 0)):
           lcm = greater
           break
       greater += 1

   return lcm

num1 = 54
num2 = 24

print("The L.C.M. is", compute_lcm(num1, num2))

Output

The L.C.M. is 216

Note: To test this program, change the values of num1 and num2.

This program stores two number in num1 and num2 respectively. These numbers are passed to the compute_lcm() function. The function returns the L.C.M of two numbers.

In the function, we first determine the greater of the two numbers since the L.C.M. can only be greater than or equal to the largest number. We then use an infinite while loop to go from that number and beyond.

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.

The above program is slower to run. We can make it more efficient by using the fact that the product of two numbers is equal to the product of the least common multiple and greatest common divisor of those two numbers.

Number1 * Number2 = L.C.M. * G.C.D.

Here is a Python program to implement this.

Program to Compute LCM Using GCD

# Python program to find the L.C.M. of two input number

# This function computes GCD 
def compute_gcd(x, y):

   while(y):
       x, y = y, x % y
   return x

# This function computes LCM
def compute_lcm(x, y):
   lcm = (x*y)//compute_gcd(x,y)
   return lcm

num1 = 54
num2 = 24 

print("The L.C.M. is", compute_lcm(num1, num2))

The output of this program is the same as before. We have two functions compute_gcd() and compute_lcm(). We require G.C.D. of the numbers to calculate its L.C.M.

So, compute_lcm() calls the function compute_gcd() to accomplish this. G.C.D. of two numbers can be calculated efficiently using the Euclidean algorithm.

Click here to learn more about methods to calculate G.C.D in Python.

How do you find HCF and LCM in Python?

Algorithm.
Initialize HCF = 1..
Run a loop in the iteration of (i) between [1, min(num1, num2)].
Note down the highest number that divides both num1 & num2..
If i satisfies (num1 % i == 0 && num2 % i == 0) then new value of HCF is i..
Use lcm formula :- (num1*num2) / hcf..
Print the output..

How do you find the HCF in Python?

num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) # printing the result for the users. print("The H.C.F. of", num1,"and", num2,"is", calculate_hcf(num1, num2))

Is LCM a function in Python?

The math module in Python contains a number of mathematical operations. Amongst some of the most important functions in this module is the lcm() function which returns the least common multiple of the specified integer arguments. The lcm function was newly introduced in the Python version 3.9. 0.

What is GCD and LCM in Python?

Write a Python program to Compute the greatest common divisor (GCD) and least common multiple (LCM) of two integer. This python program allows the user to enter two positive integer values and compute GCD using while loop.