The Ruby lcm()
function returns the LCM of two integers.
The LCM or least common multiple is the smallest number that is the multiple of all the numbers used to calculate LCM.
Figure 1, below, shows the visual representation of the lcm()
function.
num_1.lcm(num_2)
# where num_1 and num_2 are the integers whose lcm is to be calculated
If
num_1
ornum_2
or both arenon-integers
, then this function throws anerror
.
This function requires another integer, (num_2)
, as a parameter.
This function returns the LCM of two integers.
The following example shows how to use the lcm()
function in Ruby.
# both positive numbersprint "(9).lcm(12) : ", (9).lcm(12), "\n"# both negative numbersprint "(-24).lcm(-16) : ", (-24).lcm(-16), "\n"# one of the numbers is zeroprint "(0).lcm(12) : ", (0).lcm(12), "\n"# one is negative and another is positiveprint "(-9).lcm(18) : ", (-9).lcm(18), "\n"