Files
30-seconds-of-code/snippets/gcd.md
zhangfelix 1069939543 bug fix in gcd.md
There is a function-redefined bug in origin code. This PR uses an alias to fix the bug.
2020-06-19 18:29:21 +08:00

334 B

title, tags
title tags
gcd math,beginner

Calculates the greatest common divisor of a list of numbers.

Use functools.reduce() and math.gcd() over the given list.

from functools import reduce
from math import gcd as gcd_from_math

def gcd(numbers):
  return reduce(gcd_from_math, numbers)
gcd([8, 36, 28]) # 4