Files
30-seconds-of-code/snippets/lcm.md
Isabelle Viktoria Maciohsek 1ad51ba8ee Update lcm tags
2020-11-02 19:31:15 +02:00

400 B

title, tags
title tags
lcm math,list,intermediate

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