Files
30-seconds-of-code/snippets/gcd.md
2019-08-20 10:09:53 +03:00

338 B

title, tags
title tags
gcd math

Calculates the greatest common divisor of a list of numbers.

Uses the reduce function from the inbuilt module functools. Also uses the math.gcd function over a list.

from functools import reduce
import math

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