Reference · Math

math.gcd

signature
math.gcd(int | list...) -> int

What 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))
output
6 6 61 6

Edge cases

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))
output
5 0 7 0 02 2

Errors

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.

this one fails
print(math.gcd(4, 6.5))
output
line 1: `math.gcd` expects an int as argument 2, got float
this one fails
print(math.gcd(12, [18]))
output
line 1: `math.gcd` expects an int as argument 2, got list

Chaining

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}`)
output
662/3

See also

math.lcm, math.isqrt, math.factorial

Thread
7Useful? Like it, or add to its thread below.

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