Python | Find HCF using function

Let us take a look at all the different methods that we have to find the HCF using Python functions

Method 1: Using for loop

def calc_HCF(x, y):
    if x > y:
        smaller = y
    else:
        smaller = x
    for i in range(1, smaller+1):
        if((x % i == 0) and (y % i == 0)):
            hcf = i 
    return hcf

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("The H.C.F. is", calc_HCF(num1, num2))

Output:

Enter first number: 32
Enter second number: 6
The H.C.F. is 2

Explanation:

Here, the compute calc_HCF() method receives two numbers stored in the variables num1 and num2. The function calculates and returns the H.C.F. for these two integers. Since the H.C.F. can only be less than or equal to the lowest number, we must first choose the smaller of the two integers in the function. Then, to move from 1 to that number, we employ a loop.

We verify that our number accurately divides both of the input integers with each iteration. If so, the number is saved as H.C.F. The greatest integer that divides both numbers exactly is what remains at the conclusion of the loop.

Method 2: Using subtraction

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

while num1 != num2:
    if num1 > num2:
        num1 -= num2
    else:
        num2 -= num1

print("HCF of", a, "and", b, "is", num1)

Explanation:

In this approach, a while loop is first executed up until num1 and num2 are not equal. Then we looked at the condition that says to do this if num1>num2 If it doesn’t work, we do num2 = num2 – num. Otherwise, we conduct num1 = num1 – num2. The loop’s conclusion is reached, and both num1 and num2 store HCF.

Method 3: Using recursion

def calc_HCF(num1, num2):
    
    if num1 == 0 or num2 == 0:
        return num1 + num2
   
    if num1 == num2:
        return num1
    
    if num1 > num2:
        return calc_HCF(num1 - num2, num2)
    else:
        return calc_HCF(num1, num2 - num1)


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("HCF of", num1, "and", num2, "is", calc_HCF(num1, num2))

Explanation:

Here, using this method, we determined whether the inputs were all zeros before returning the total of the two values. If both of the inputs are equal, we can return any of the two values. If num1 is bigger than num2, we recursively run calc_HCF(num1 – num2, num2), and if not, we recursively call calc_HCF(num1, num2-num1)

Method 4: Using the modulo operator

def calc_HCF(a, b):
    return b == 0 and a or calc_HCF(b, a % b)


num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print("HCF of", num1, "and", num2, "is", calc_HCF(num1, num2))

Output:

Enter first number: 32
Enter second number: 6
HCF of 32 and 6 is 2

Explanation:

Here again, we used the concept of recursion, but here every time we pass the value computed by a % b as a second argument to the function.

Method 5: Using AND / OR to handle negative numbers

def calc_HCF(a, b):
    return b == 0 and a or calc_HCF(b, a % b)

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

num1 >= 0 and num1 or -num1
num2 >= 0 and num2 or -num2

print("HCF of", num1, "and", num2, "is", calc_HCF(num1, num2))

Explanation

In this function, we have used the same logic for a function that we used in the previous method but the one change here is that the HCF of two numbers can never be negative, thus if any of the values are negative, simply multiply them by -1 to make them positive. Return and if b is equal to 0; otherwise, execute the function for value b, a% b, and return the value recursively.

Those are the five methods using which we can find HCF. We have also added a method to handle the negative numbers and compute their HCF.

Share:

Author: Ayush Purawr