Python Function Reference

math.gcd

Find the greatest common divisor of integers.

Python 3.13
math.gcd(*integers)
*integers
Integers; any number of them.
int

Return value

A non-negative int; 0 if all arguments are zero or there are none.

Examples

Greatest common divisor of two numbers

Python 3.13
import math
print(math.gcd(12, 8))
4

Divisor of several numbers

Python 3.13
import math
print(math.gcd(12, 18, 30))
6

Details

The sign of the arguments is ignored: math.gcd(-12, 8) gives 4. Floats are not accepted and raise TypeError. The least common multiple is calculated by math.lcm.

See also

Where to learn