Reference · Math
math.gcd
math.gcd(int | list...) -> intWhat it does
The greatest common divisor of some ints, as Python's math.gcd: the largest int that
divides each of them. The ints come as separate arguments or as one list;
math.gcd(12, 18) and math.gcd([12, 18]) are both 6. The answer is never
negative.
Examples
print(math.gcd(12, 18), math.gcd(12, 18, 30), math.gcd([12, 18, 30]))print(math.gcd(7, 13), math.gcd(-12, 18))6 6 61 6Edge cases
- Zero divides into anything:
math.gcd(0, 5)is5, andmath.gcd(0, 0)is0. - One int gives its absolute value, and no ints at all (or an empty list) give
0, as in Python. - Negative ints count by their absolute value:
math.gcd(-4, -6)is2. - A float that is a whole number is accepted as an int.
print(math.gcd(0, 5), math.gcd(0, 0), math.gcd(7), math.gcd(), math.gcd([]))print(math.gcd(-4, -6), math.gcd(4, 6.0))5 0 7 0 02 2Errors
An argument that is not an int, or a list item that is not, is a runtime error, not an
error value; a float with a fraction and null are refused. A list may only be the
whole argument list: math.gcd(12, [18]) fails.
print(math.gcd(4, 6.5))line 1: `math.gcd` expects an int as argument 2, got floatprint(math.gcd(12, [18]))line 1: `math.gcd` expects an int as argument 2, got listChaining
A list in a pipeline is the only argument, so xs | math.gcd reduces the whole list;
a number in a pipeline is the first of two:
print([12, 18, 30] | math.gcd)print(12 | math.gcd(18))let [a, b] = [12, 18]let g = math.gcd(a, b)print(`${a / g}/${b / g}`)662/3See also
math.lcm, math.isqrt, math.factorial
Thread 0 posts
On math.gcd(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo