[issue39648] Update math.gcd() to accept "n" arguments.

Dennis Sweeney report at bugs.python.org
Sun Feb 16 15:26:50 EST 2020


Dennis Sweeney <sweeney.dennis650 at gmail.com> added the comment:

Correction correction: returning zero preserves the invariant below.

from math import gcd as GCD
from functools import reduce
from itertools import starmap, chain

def gcd(*args):
    return reduce(GCD, args, 0)

iterables = [[10, 20, 30],
             [],
             [0, 0, 0],
             [5],
             [-15, -10000000]]

a = gcd(*chain.from_iterable(iterables))
b = gcd(*starmap(gcd, iterables))
assert a == b == 5

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39648>
_______________________________________


More information about the Python-bugs-list mailing list