Is there any gcd function in python?

❮ Math Methods


Example

Find the greatest common divisor of the two integers:

#Import math Library
import math

#find the  the greatest common divisor of the two integers
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
print (math.gcd(-12, -36))
print (math.gcd(5, 12))
print (math.gcd(10, 0))
print (math.gcd(0, 34))
print (math.gcd(0, 0))

Try it Yourself »


Definition and Usage

The math.gcd() method returns the greatest common divisor of the two integers int1 and int2.

GCD is the largest common divisor that divides the numbers without a remainder.

GCD is also known as the highest common factor (HCF).

Tip: gcd(0,0) returns 0.


Syntax

Parameter Values

ParameterDescription
int1 Required. The first integer to find the GCD for
int2 Required. The second integer to find the GCD for

Technical Details

Return Value:An int value, representing the greatest common divisor (GCD) for two integers
Python Version:3.5

❮ Math Methods


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The Highest Common Factor (HCF), also called gcd, can be computed in python using a single function offered by math module and hence can make tasks easier in many situations.

    Naive Methods to compute gcd

    Way 1: Using Recursion

    Python3

    def hcfnaive(a, b):

        if(b == 0):

            return abs(a)

        else:

            return hcfnaive(b, a % b)

    a = 60

    b = 48

    print("The gcd of 60 and 48 is : ", end="")

    print(hcfnaive(60, 48))

    Output

    The gcd of 60 and 48 is : 12
    

    Way 2: Using Loops 

    Python3

    def computeGCD(x, y):

        if x > y:

            small = y

        else:

            small = x

        for i in range(1, small + 1):

            if((x % i == 0) and (y % i == 0)):

                gcd = i

        return gcd

    a = 60

    b = 48

    print ("The gcd of 60 and 48 is : ", end="")

    print (computeGCD(60,48))

    Output

    The gcd of 60 and 48 is : 12
    

    Way 3: Using Euclidean Algorithm 

    Python3

    def computeGCD(x, y):

       while(y):

           x, y = y, x % y

        return abs(x)

    a = 60

    b = 48

    print ("The gcd of 60 and 48 is : ",end="")

    print (computeGCD(60, 48))

    Output:

    The gcd of 60 and 48 is : 12
    • Both numbers are 0, gcd is 0
    • If only either number is Not a number, Type Error is raised.

    How do you write a GCD function in Python?

    gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments..
    Syntax: math.gcd(x, y).
    Parameter: ... .
    Returns: An absolute/positive integer value after calculating the GCD of given parameters x and y..

    What library is required to use the GCD function in Python?

    In order to compute GCD in Python we need to use the math function that comes in built in the Python library.

    Is GCD an inbuilt function?

    std::gcd | C++ inbuilt function for finding GCD C++ has the built-in function for calculating GCD. This function is present in header file. Syntax for C++14 : Library: 'algorithm' __gcd(m, n) Parameter : m, n Return Value : 0 if both m and n are zero, else gcd of m and n.

    How do you find the GCF in Python?

    Therefore, we can set up an algorithm to find the GCF as follows:.
    Take two given integers x and y..
    Replace the larger one with the difference of the two..
    Continue this process until the difference is equal to zero (i.e. the two numbers are the same).
    GCD is the value of x or y in the last step..