math.gcd()
Anonymous contributor
Published Aug 21, 2024
Contribute to Docs
The math.gcd()
function in Python returns the Greatest Common Divisor (GCD) of two or more integers. The GCD is the largest positive integer that divides all of the given numbers without leaving a remainder.
Syntax
math.gcd(*integers)
*integers
: This represents the integers for which to compute the GCD.
Example
The example below uses the math.gcd()
function to return the greatest common divisor of the specified integers:
import math# GCD of two integers.print(math.gcd(54, 24))# GCD of more than two integers.print(math.gcd(54, 108, 216))# GCD of zero and non-zero integers.print(math.gcd(54, 0))
The output of the example code above is:
65454
Note: If
math.gcd()
function is called with no arguments or if all arguments are zero (0
), it will return zero (0
).
Codebyte Example
Run the following example that uses the math.gcd()
function to understand how it works:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.