I know that, in Ruby, you can use the Integer#lcm method to get the least common multiple of two numbers. For example:
10.lcm(15)
# => 30
Is there an efficient (or built-in to the core or stdlib) way to get the least common multiple of all the integers in a given array? For example:
[5, 3, 10, 2, 20].lcm
# => 60
Any operation that takes two operands can be applied iteratively to a collection by folding it: Enumerable#inject/reduce. To cover the empty case, pass as first argument the identity element of the operation, which is
1for the least common denominator.Which can be also written as: