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

19 lines
338 B
Markdown

---
title: gcd
tags: 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.
```py
from functools import reduce
import math
def gcd(numbers):
return reduce(math.gcd, numbers)
```
```py
gcd([8,36,28]) # 4
```