Python math.gcd() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.5+ / 3.9+

Definition

math.gcd(*integers) returns the greatest common divisor of the given integers as a non-negative int.

Added for two integers in Python 3.5, it was extended in Python 3.9 to accept zero, one, or many integer arguments (math.gcd() == 0).

math.gcd() is a function in Python's standard math module: import it with import math and call math.gcd(...), or import the name directly with from math import gcd. Added in Python 3.5 (two args); zero, one, or 3+ args since Python 3.9.

Syntax

Import math and pass two integers (or any count in Python 3.9+).

python
import math

d = math.gcd(a, b)
CallWhat it does
math.gcd(12, 18)Returns 6.
math.gcd(-24, 36)Returns 12 (always non-negative).
math.gcd(0, 0)Returns 0.
math.gcd(12.0, 6)Raises TypeError: floats are rejected.

Notice math.gcd(0, 0) returns 0 rather than raising an error, and negative arguments return a positive divisor. Pairs with math.lcm() (Python 3.9+).

Function signatures
python
math.gcd(*integers)
# 2 args in Python 3.5-3.8; 0, 1, or more args in Python 3.9+

Parameters

ParameterDescription
*integersInteger values (int only). Passing floats or non-integers raises TypeError.

Return Value

Return ValueDescription
intNon-negative greatest common divisor; 0 if all arguments are 0 (or none given in 3.9+).

Python Examples

Example 1 — Simplify Fractions to Lowest Terms With math.gcd()

Divide numerator and denominator by their GCD using exact floor division //.

python
import math

for num, den in [(12, 18), (100, 75), (-24, 36)]:
    g = math.gcd(num, den)
    print(f"{num}/{den} -> {num // g}/{den // g} (gcd={g})")

How It Works

  1. Divide out common factors. Dividing both numbers by gcd(12, 18) = 6 reduces 12/18 cleanly to 2/3.
  2. Exact integer division. Using // keeps both numerator and denominator as exact ints.
  3. Negative signs stay clean. Because math.gcd(-24, 36) is positive 12, the negative sign stays on the numerator.

Example 2 — Find the Largest Square Tile Size for Rectangular Rooms

Compute the maximum square tile edge that tiles a rectangle evenly without cutting.

python
import math

for width, height in [(240, 360), (168, 210)]:
    tile = math.gcd(width, height)
    count = (width // tile) * (height // tile)
    print(f"{width} by {height} -> {tile} x {tile} tile ({count} tiles)")

How It Works

  1. Divides both sides. Any square tile fitting cleanly into both dimensions must divide both width and height.
  2. Maximum size via GCD. math.gcd(240, 360) finds 120, splitting the floor into 2x3 = 6 tiles.
  3. Integer count. Multiplying quotients gives total tile count cleanly.

Example 3 — Find GCD of Multiple Numbers (3.8+ Safe) and Catch Floats

Use functools.reduce alongside 3.9+ multi-arg support, test zeros, and trap float TypeError.

python
import functools
import math
import sys

nums = [24, 36, 60]
if sys.version_info >= (3, 9):
    multi = math.gcd(*nums)
else:
    multi = functools.reduce(math.gcd, nums)

print("gcd of", nums, "is", multi)
print("gcd(0, 0):", math.gcd(0, 0))

try:
    math.gcd(12.0, 6)
except TypeError:
    print("gcd() requires integers, not floats")

How It Works

  1. Version-aware multi-arg call. On Python 3.9+, math.gcd(*nums) handles arbitrary argument counts; older 3.8 falls back to functools.reduce.
  2. All zeros return zero. math.gcd(0, 0) returns 0 without error.
  3. Strict integer check. Passing float 12.0 raises TypeError.

Frequently Asked Questions

Python 3.9 added support for zero, one, or three or more arguments. Python 3.5-3.8 accept only two arguments.
No. Even if inputs are negative, math.gcd(-12, -18) returns positive 6.
0. And math.gcd() with no arguments returns 0 in Python 3.9+.
Because gcd operates strictly on exact integer types (int, or objects with __index__()), not floats.
In Python 3.9+ use math.lcm(). On older versions compute abs(a * b) // math.gcd(a, b) when nonzero.

Next: math.hypot()

Return the Euclidean norm sqrt(sum(x*x)) without overflow.

hypot() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful