Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.

Files

Latest commit

 

History

History
24 lines (19 loc) · 480 Bytes

lcm.md

File metadata and controls

24 lines (19 loc) · 480 Bytes
title type tags cover dateModified
Least common multiple
snippet
math
list
fruit-feast
2020-11-02 19:31:15 +0200

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.
from functools import reduce
from math import gcd

def lcm(numbers):
  return reduce((lambda x, y: int(x * y / gcd(x, y))), numbers)
lcm([12, 7]) # 84
lcm([1, 3, 4, 5]) # 60