19 lines
338 B
Markdown
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
|
|
``` |