Python gcd

The Python gcd math function returns the greatest common divisor of two given arguments. In this section, we discuss how to use gcd function in Python Programming language with an example.

Syntax of a Python gcd Function

The syntax of the gcd Function in Python Programming Language is

math.gcd(x, y);
  • X: Please specify the X value here.
  • Y: Please specify the Y value here. The Python gcd Function uses these X and Y arguments to find the greatest common divisor.

NOTE:

  • If both the X and Y arguments are Zero, the gcd function returns the output as Zero.
  • If either the X or Y argument is Zero, gcd function returns the Non-zero value as the greatest common divisor
  • Next, if either the X or Y argument is a Decimal, or If the X value or Y value argument is not a number, gcd function returns TypeError.

Python gcd Function Example

The math gcd function returns the greatest common divisor of two given arguments. In this gcd example, We are going to find the greatest common divisor of different data types and display the output

# Python GCD Function

import math

Tup = (10, 20, 12, -40 , 50) # Tuple Declaration
Lis = [-98, 32, -39, -42 , 15] # List Declaration

print('Calculating GCD of Positive Number = %d' %math.gcd(2, 3))
print('Calculating GCD of Negative Number = %d' %math.gcd(-2, 3))

print('Calculating GCD of Zero = %d' %math.gcd(0, 0))
print('Calculating GCD of Zero & Non-Zero element = %d' %math.gcd(2, 0))
print('Calculating GCD of Non-Zero & Zero element = %d' %math.gcd(0, 4))

print('Calculating GCD of of Tuple Item = %d' %math.gcd(Tup[2], 4))
print('Calculating GCD of of Tuple Item = %d' %math.gcd(Tup[2], -6))
print('Calculating GCD of List Item = %d' %math.gcd(Lis[4], 5))
print('Calculating GCD of List Item = %d' %math.gcd(Lis[4], -45))

print('Calculating GCD of Multiple Number = %d' %math.gcd(10 + 20 - 12, 40))

print('Calculating GCD of String Value = ', math.gcd('2', 3))
Python GCD Function
  1. Within the first two statements, We passed both the Positive integer and negative integer as the Python gcd Function arguments. From the above screenshot, see that the gcd Function is returning output.
  2. Within the next three statements, We passed both the Zero as the first and second arguments. As we said before, the gcd Function is returning a non-zero argument as the result.
  3. Following four statements, We used the Python Tuple and Python List items as first arguments and Positive and negative integer values as the second argument for gcd Math function. If you observe the above screenshot, the gcd function is working perfectly on them.
  4. Next, We assigned multiple values as the first arguments, and the gcd Function worked without any issue.
  5. Next, We tried Python gcd Function on String value, and it returns TypeError as output
  6. Within the Python IDE, We passed the Decimal values as the Function arguments. As we said earlier, the gcd Function is returning the TypeError: ‘float’ object cannot be interpreted as an integer.