Python math.gcd() – Greatest Common Divisor

Python math.gcd()

math.gcd(*integers) function returns the Greatest Common Divisor (GCD) of the integers passed as arguments.

Syntax

The syntax to call gcd() function is

math.gcd(*integers)

where

ParameterRequiredDescription
*integersNoNone, one or multiple integer values.

If no integer value is passed as argument to gcd(), then the return value is 0.

If only one integer is passed to gcd(), then then same value is returned as GCD.

If any non-integral value is passed as argument to gcd(), then the function raises TypeError.

Examples

GCD of two integers: 10 and 25.

Python Program

import math

result = math.gcd(10, 25)
print('gcd() :', result)
Run

Output

gcd() : 5

GCD of one integer value.

Python Program

import math

result = math.gcd(25)
print('gcd() :', result)
Run

Output

gcd() : 25

GCD of arguments containing floating point values.

Python Program

import math

result = math.gcd(25.5, 2.6)
print('gcd() :', result)
Run

Output

TypeError: 'float' object cannot be interpreted as an integer

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.gcd() function.