How to Find HCF or GCD using Python?


In this article, we will show you how to find the HCF (Highest Common Factor) or GCD (Greatest Common Factor) in Python. Below are the various methods to accomplish this task:

  • Using For Loop

  • Using Euclidean Algorithm

  • Using While Loop

  • Using Recursion(Naive method)

  • Handling Negative Numbers in HCF

What is H.C.F. or G.C.D?

The largest positive integer that perfectly divides the two given numbers is known as the highest common factor (H.C.F.) or greatest common divisor (G.C.D.).

Example - The HCF of 12 and 14, is 2.

Using For Loop

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Create a variable to store the input number 1.

  • Create another variable to store the input number 2.

  • Use the if conditional statement to check whether the first number is greater than the second.

  • Considering the second number as the smaller number if the condition is true.

  • Else considering the first number as a smaller number.

  • Use for loop to traverse from 1 to small number using the range() function (The range() function returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number).

  • Use the if conditional statement to check whether both the first and second numbers divide exactly by index (i) with the modulo operator % (returns the remainder), and operator(returns true if both the conditions are true)

  • Assign the corresponding index(i) value as HCF if the condition is true

  • Print the resultant HCF of input number 1 & input number 2.

Example

The following program returns the HCF of input number 1 & input number 2 using for loop -

# input number 1 inputNumber_1 = 30 # input number 2 inputNumber_2 = 14 # checking whether the first number is greater than # the second one if inputNumber_1 > inputNumber_2: # assigning the second number as a smaller number small_num = inputNumber_2 else: # else assigning the first number as a smaller number small_num = inputNumber_1 # traversing from 1 to the small number range for i in range(1, small_num+1): # checking whether both the first and second numbers divide exactly by index (i) if((inputNumber_1 % i == 0) and (inputNumber_2 % i == 0)): # assigning the index(i) value as HCF if the condition is true resultHcf = i # printing the HCF of input number 1 & input number 2 print("The H.C.F of", inputNumber_1, "and", inputNumber_2, "=", resultHcf)

Output

On executing, the above program will generate the following output −

The H.C.F of 30 and 14 = 2

Using Euclidean Algorithm

Example

The following program returns the HCF of input number 1 & input number 2 using Euclidean Algorithm –

# creating a function that returns the HCF of two numbers passed to it def calculateHcf(p, q): # traversing the loop until q times while(q): # assigning q value to p, p % q value to q p, q = q, p % q # returning p value(i.e, inputNumber_2) return p # input number 1 inputNumber_1 = 15 # input number 2 inputNumber_2 = 4 # calling the calculateHcf() function by passing both the input numbers to it resultHcf = calculateHcf(inputNumber_1, inputNumber_2) # printing the HCF of input number 1 & input number 2 print("The H.C.F of", inputNumber_1, "and", inputNumber_2, "=", calculateHcf(inputNumber_1, inputNumber_2))

Output

On executing, the above program will generate the following output −

The H.C.F of 15 and 4 = 1

Using While Loop (Repeated Subtraction)

Example

The following program returns the HCF of input number 1 & input number 2 using a while loop (Repeated Subtraction approach) –

# input number 1 inputNumber_1 = 40 # input number 2 inputNumber_2 = 5 # storing both the numbers in temporary variables # for not getting disturbed temp_1 = inputNumber_1 temp_2 = inputNumber_2 # traversing the loop until both the numbers are not equal while inputNumber_1 != inputNumber_2: # checking whether inputNumber_1 is greater than inputNumber_2 if inputNumber_1 > inputNumber_2: # assigning inputNumber_1-inputNumber_2 value to inputNumber_1 inputNumber_1 -= inputNumber_2 else: # else assigning inputNumber_2-inputNumber_1 value to inputNumber_2 inputNumber_2 -= inputNumber_1 # printing the HCF of input number 1 & input number 2 print("The H.C.F of", temp_1, "and", temp_2, "=", inputNumber_1)

Output

On executing, the above program will generate the following output −

The H.C.F of 40 and 5 = 5

Using Recursion(Naive method)

Example

The following program returns the HCF of input number 1 & input number 2 using recursion -

# creating a function that returns the HCF of two numbers passed to it def calculateHcf(num_1, num_2): # checking whether the second number is equal to 0 if(num_2 == 0): # returning the absolute value of the first number if the condition is true return abs(num_1) else: # else returning the resultant HCF by calling the calculateHcf() function # by passing num_2, value of num_1 % num_2 as arguments return calculateHcf(num_2, num_1 % num_2) # input number 1 inputNumber_1 = 32 # input number 2 inputNumber_2 = 6 # calling the calculateHcf() function by passing both the input numbers to it print("The H.C.F of", inputNumber_1, "and", inputNumber_2, "=", calculateHcf(inputNumber_1, inputNumber_2))

Output

On executing, the above program will generate the following output −

The H.C.F of 32 and 6 = 2

Handling Negative Numbers in HCF

The HCF of two numbers can never be negative, so if any of the numbers are negative, simply multiply them by -1 to make them positive.

In this method, the complexity of repeated subtraction is improved by the efficient use of the modulo operator(%) in the euclidean algorithm.

The following program returns the HCF of input number 1 & input number 2 using while by handling the negative numbers in HCF –

Example

# creating a function to calculate the hcf def calculateHcf(x, y): return y == 0 and x or calculateHcf(y, x % y) # input number 1 inputNumber_1 = -4 # input number 2 inputNumber_2 = 15 # If the user types a negative number, we simply change it to a positive number. # HCF is the highest positive number that divides both numbers # -4 & 15 --> HCF = 1 (as the highest number that divides both) # -4 & -15 --> HCF = 1 (as the highest number that divides both) inputNumber_1 >= 0 and inputNumber_1 or -inputNumber_1 inputNumber_2 >= 0 and inputNumber_2 or -inputNumber_2 # calling the calculateHcf() function by passing both the input numbers to it print("The H.C.F of", inputNumber_1, "and", inputNumber_2, "=", calculateHcf(inputNumber_1, inputNumber_2))

Output

On executing, the above program will generate the following output −

The H.C.F of -4 and 15 = 1

Conclusion

In this article, we learned how to find the gcd/hcf of a number in Python using four different methods. We also learned how to deal with negative numbers when computing HCF.

Updated on: 28-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements