In this Python program, we will learn how to find the HCF(Highest Common Factor) or GCD (Greatest Common Divisor) using the euclidean algorithm and recursion.
In mathematics, the Euclidean algorithm, or Euclid's algorithm, is an efficient method for computing the greatest common divisor (GCD) of two integers (numbers) is the largest number that divides them both without a remainder.
The algorithm is based on the below facts:
If we divide the larger number by smaller number and take the remainder and now divide that smaller number by this remainder. Repeat until the remainder is equal to 0.
For example, if we want to find the H.C.F. of 60 and 36, we divide 60 by 36. The remainder is 24. Now, we divide 36 by 24 and the remainder is 12. Again, we divide the 24 by 12 and the remainder is 0. Hence, 12 is the required H.C.F.
Here is the code of the program to find the HCF(Highest Common Factor) or GCD (Greatest Common Divisor) using the Euclidean algorithm and recursion.
# Python program to find H.C.F of two numbers Using Euclidean algorithm and Recursion # Define a Recursive Function def compute_HCF(x, y): if (y == 0): return x else: return compute_HCF(y, x % y) # To Take Input from the User num1 = int(input("Enter the First Number: ")) num2 = int(input("Enter the Second Number: ")) print("\nThe HCF is", compute_HCF(num1, num2))
Enter the First Number: 60
Enter the Second Number: 36
The HCF is 12
Enter the First Number: 10
Enter the Second Number: 12
The H.C.F. is 2