Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython Program to Find the Gcd of Two Numbers

Python Program to Find the Gcd of Two Numbers

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

Python Program to Find the Gcd of 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




# Python code to demonstrate the working of gcd()
# importing "math" for mathematical operations
import math
 
# prints 12
print("The gcd of 60 and 48 is : ", end="")
print(math.gcd(60, 48))


Output

The gcd of 60 and 48 is : 12

Time complexity:  O(log(min(a,b))), as it uses the Euclidean Algorithm for finding the GCD.
Auxiliary Space: O(log(n)), as it uses recursion and the maximum depth of recursion is log(n).

Python Program to Find the Gcd of Two Numbers Using Recursion 

This code calculates the greatest common divisor (gcd) of two numbers using a recursive algorithm. It uses the Euclidean algorithm to find the gcd and returns the result.

Python3




# Python code to demonstrate naive
# method to compute gcd ( recursion )
 
 
def hcf(a, b):
    if(b == 0):
        return a
    else:
        return hcf(b, a % b)
 
a = 60
b = 48
 
# prints 12
print("The gcd of 60 and 48 is : ", end="")
print(hcf(60, 48))


Output

The gcd of 60 and 48 is : 12

Time complexity:  O(log(min(a,b))), as it uses the Euclidean Algorithm for finding the GCD.
Auxiliary Space: O(log(n)), as it uses recursion and the maximum depth of recursion is log(n).

Find the Gcd of Two Numbers 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




# Recursive function to return gcd of a and b
def gcd(a, b):
 
    # Everything divides 0
    if (a == 0):
        return b
    if (b == 0):
        return a
 
    # base case
    if (a == b):
        return a
 
    # a is greater
    if (a > b):
        return gcd(a-b, b)
    return gcd(a, b-a)
 
# Driver program to test above function
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

Time complexity: O(log(min(a,b))), as it uses the Euclidean algorithm which has a time complexity of O(log(min(a,b))). 
Auxiliary Space: O(1), as it only uses a few variables and does not require any additional data structures.

Find GCD with Lambda function

In this code, the lambda function gcd takes two arguments a and b. It uses the same recursive algorithm as the previous code example to calculate the gcd. The function returns a if b is equal to 0, otherwise it calls itself with b and a % b. Finally, the gcd is printed using the lambda function.

Python3




gcd = lambda a, b: a if b == 0 else gcd(b, a % b)
 
a = 60
b = 48
 
# Prints 12
print("The gcd of 60 and 48 is:", gcd(a, b))


Output:

The gcd of 60 and 48 is: 12

RELATED ARTICLES

Most Popular

Recent Comments