Files
30-seconds-of-code/snippets/lcm.md
Isabelle Viktoria Maciohsek cbc78ee450 Bake dates into snippets
2021-06-13 19:38:10 +03:00

476 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
lcm math,list,intermediate 2018-01-08T22:30:17+02:00 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