Files
30-seconds-of-code/snippets/python/s/lcm.md
Angelos Chalaris 4d0316a062 Update covers
2023-05-07 22:25:00 +03:00

497 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Least common multiple snippet python
math
list
chess-pawns 2020-11-02T19:31:15+02:00

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