Greatest Common Divisor in Python
Calculate GCF, GCD, and HCF of a set of two or more numbers and see the work using factorization.
What is Greatest Common Divisor?
The greatest common divisor (also known as Greatest Common Factor, Greatest Common Denimonator, and Highest Common Factor) of a set of whole numbers is the largest positive integer that divides evenly into all numbers with zero remainder. For example, for the set of numbers 18, 30 and 42 the GCF = 6.
How to find GCD in Python?
Python has a built-in gcd function in the math module which can be used for this purpose.
def gcd(x, y):
import math
return math.gcd(x, y)
Example
number_1
number_2
f"The greatest common divisor of {number_1} and {number_2} is {gcd(int(number_1), int(number_2))}."