Find the greatest common divisor of two numbers in python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given two numbers. The task is to find the GCD of the two numbers.

    Using STL :

    In Python, the math module contains a number of mathematical operations, which can be performed with ease using the module. math.gcd() function compute the greatest common divisor of 2 numbers mentioned in its arguments.

    Syntax: math.gcd(x, y)

    Parameter:

    x : Non-negative integer whose gcd has to be computed.

    y : Non-negative integer whose gcd has to be computed.

    Returns: An absolute/positive integer value after calculating the GCD of given parameters x and y.

    Exceptions: When Both x and y are 0, function returns 0, If any number is a character, Type error is raised.

    Python3

    import math

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

    print(math.gcd(60, 48))

    Output

    The gcd of 60 and 48 is : 12
    

    Using Recursion :

    Python3

    def hcf(a, b):

        if(b == 0):

            return a

        else:

            return hcf(b, a % b)

    a = 60

    b = 48

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

    print(hcf(60, 48))

    Output

    The gcd of 60 and 48 is : 12
    

    Using Euclidean Algorithm :

    The Euclid’s algorithm (or Euclidean Algorithm) is a method for efficiently finding the greatest common divisor (GCD) of two numbers. The GCD of two integers X and Y is the largest number that divides both of X and Y (without leaving a remainder).

    Pseudo Code of the Algorithm-

    1. Let  a, b  be the two numbers
    2. a mod b = R
    3. Let  a = b  and  b = R
    4. Repeat Steps 2 and 3 until  a mod b  is greater than 0
    5. GCD = b
    6.  Finish

    Python3

    def gcd(a, b):

        if (a == 0):

            return b

        if (b == 0):

            return a

        if (a == b):

            return a

        if (a > b):

            return gcd(a-b, b)

        return gcd(a, b-a)

    a = 98

    b = 56

    if(gcd(a, b)):

        print('GCD of', a, 'and', b, 'is', gcd(a, b))

    else:

        print('not found')

    Output

    GCD of 98 and 56 is 14
    


    Greatest Common Divisor (GCD) is a mathematical term to find the greatest common factor that can perfectly divide the two numbers. A GCD is also known as the Highest Common Factor (HCF). For example, the HCF/ GCD of two numbers 54 and 24 is 6. Because 6 is the largest common divisor that completely divides 54 and 24.

    Find the greatest common divisor of two numbers in python

    GCD Using gcd() Function

    In python, a gcd() is an inbuilt function offered by the math module to find the greatest common divisor of two numbers.

    Syntax

    Where a and b are the two integer number passes as an argument to the function gcd().

    Let's create a program to print the GCD of two number using the inbuilt function of math.gcd() in python.

    math_fun.py

    Output:

    Find the greatest common divisor of two numbers in python

    In the above example, math.gcd() function generates the GCD of two given numbers. In the gcd() function, a and b pass as an argument that returns the greatest common divisor of two integer numbers, completely dividing the numbers.

    GCD Using recursion

    Recursion is a memory consuming function defined in python that calls itself via self-referential expression. It means that the function will continuously call and repeat itself until the defined condition is met to return the greatest common divisor of the number.

    Pseudo Code of the Algorithm

    Step 1: Take two inputs, x and y, from the user.

    Step 2: Pass the input number as an argument to a recursive function.

    Step 3: If the second number is equal to zero (0), it returns the first number.

    Step 4: Else it recursively calls the function with the second number as an argument until it gets the remainder, which divides the second number by the first number.

    Step 5: Call or assign the gcd_fun() to a variable.

    Step 6: Display the GCD of two numbers.

    Step 7: Exit from the program.

    Let's understand the program to find the GCD of two number using the recursion.

    gcdRecur.py

    Output:

    Find the greatest common divisor of two numbers in python

    GCD Using the Loop

    Let's create program to find the GCD of two number in python using the loops.

    gcdFile.py

    Output:

    Find the greatest common divisor of two numbers in python

    As we can see in the above program, we take two values as input and pass these numbers to the GCD_Loop () function to return a GCD.

    GCD Using Euclid's algorithm or Euclidean Algorithm

    Euclid's algorithm is an efficient method to find the greatest common divisor of two numbers. It is the oldest algorithm that divides the greater number into smaller ones and takes the remainder. Again, it divides the smaller number from the remainder, and this algorithm continuously divides the number until the remainder becomes 0.

    For example, suppose we want to calculate the H.C.F of two numbers, 60 and 48. Then we divide the 60 by 48; it returns the remainder 12. Now we again divide the number 24 by 12, and then it returns the remainder 0. So, in this way, we get the H.C.F is 12.

    Pseudo Code of the Euclid Algorithm

    Step 1: There are two integer numbers, such as a and b.

    Step 2: if a = 0, then the GCD(a, b) is b.

    Step 3: if b = 0, the GCD(a, b) is a.

    Step 4: a mod b find the

    Step 5: Suppose a = b and b = R

    Step 6: Repeat steps 4 and 3 until a mod b is equal or greater than 0.

    Step 7: GCD = b and then print the result.

    Step 8: Stop the program.

    Let's find the H.C.F or GCD of two numbers using Euclid's algorithm in python.

    Euclid.py

    Output:

    Find the greatest common divisor of two numbers in python

    How do you get the gcd of two numbers in Python?

    Using Euclidean Algorithm :.
    Let a, b be the two numbers..
    a mod b = R..
    Let a = b and b = R..
    Repeat Steps 2 and 3 until a mod b is greater than 0..
    GCD = b..
    Finish..

    What is greatest common divisor in Python?

    Python math.gcd() Method 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).

    How do you find the gcd of two numbers in Python using loops?

    Python Program to find GCD of Two Numbers Example 1 Within the While loop, we used the If Statement to check whether a%i and a % i remainder equal to zero or not. If true, Highest Common Factor = I otherwise skip that value.

    How do you find the gcd of two numbers?

    The steps to calculate the GCD of (a, b) using the LCM method is:.
    Step 1: Find the product of a and b..
    Step 2: Find the least common multiple (LCM) of a and b..
    Step 3: Divide the values obtained in Step 1 and Step 2..
    Step 4: The obtained value after division is the greatest common divisor of (a, b)..