Files
30-seconds-of-code/snippets/lcm.md
Angelos Chalaris 89c857ece0 Update code in lcm
2020-01-03 13:08:53 +02:00

22 lines
404 B
Markdown

---
title: lcm
tags: math,list,recursion,advanced
---
Returns the least common multiple of a list of numbers.
Use `functools.reduce()`, `math.gcd()` and `lcm(x,y) = x * y / gcd(x,y)` over the given list.
```py
from functools import reduce
from math import gcd
def lcm(numbers):
return reduce((lambda x, y: int(x * y / gcd(x, y))), numbers)
```
```py
lcm([12, 7]) # 84
lcm([1, 3, 4, 5]) # 60
```