Python gcd

The Python math gcd function returns the greatest common divisor of two given arguments. In this section, we discuss how to use the gcd function with an example. The syntax of the gcd Function is as shown below.

math.gcd(x, y);

Please specify the X and Y values here. The Python gcd Function uses these X and Y arguments to find the greatest common divisor.

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

Python gcd Function Example

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

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 math function Example
  1. In this gcd() function example, we used the Tuple and List items as the first arguments and Positive and negative integer values as the second argument for this Math function. If you observe the above screenshot, it is working perfectly on them.
  2. Next, We assigned multiple values as the first arguments, and it worked without any issues.
  3. Next, We tried gcd Function on String value, which returns TypeError as output.
  4. Within the Python IDE, we passed the Decimal values as the Function arguments. As we said earlier, it is returning the TypeError: ‘float’ object cannot be interpreted as an integer. So, we can use ceil(), floor(), or round() function to get the nearest integer and then apply the gcd() function on the return value.

How to find the gcd of a list of numbers?

We can use the built-in Python gcd() function on lists, dictionaries, sets, etc. However, we must use the list comprehension, for loop, or map function to iterate over the list items. Next, apply the gcd() function on each iteration.

In the following example, we use a list comprehension to iterate over the list of positive integers. As the gcd() function requires two arguments, we use the list items as the first argument and set 4 as the default second parameter.

import math
numbers = [8, 10, 255, 30, 60, 70]
fact = [math.gcd(i, 4) for i in numbers]
print(fact)
[4, 2, 1, 2, 4, 2]

How to find the gcd of three or more numbers in Python?

To find the GCD of more than two numbers, we can use the reduce() function or the for loop. In the following example, we declared a list of integers and imported the reduce() function from functools.

Next, use the built-in math gcd() function as the first argument and the list of numbers as the second argument. If you observe the result, it returns 12 because it is the GCD value of all numbers.

from math import gcd
from functools import reduce

numbers = [24, 36, 48, 60, 72, 96]
result = reduce(gcd, numbers)
print(result)
12

TIP: Please refer to the Program to find the GCD of two numbers article to understand multiple approaches without using the math library.