find the least common multiple of two numbers in ruby

Here's a simple function to find the least common multiple of two integers using the lcm method in Ruby's math module:

main.rb
def lcm(a, b)
  return a.lcm(b)
end
36 chars
4 lines

You can call this function with two integers as arguments and it will return their least common multiple:

main.rb
lcm(4, 6) # returns 12
lcm(7, 9) # returns 63
46 chars
3 lines

The lcm method returns the least common multiple of two numbers using the formula (a * b) / gcd(a, b), where gcd is the greatest common divisor of the two numbers. The lcm method is a part of the Ruby's Integer class and takes one argument, which is the second number to find lcm of.

gistlibby LogSnag