Least common multiple

You are encouraged to solve this task according to the task description, using any language you may know.
Task

Compute the   least common multiple   (LCM)   of two integers.

Given   m   and   n,   the least common multiple is the smallest positive integer that has both   m   and   n   as factors.



Example

The least common multiple of   12   and   18   is   36,       because:

   12   is a factor     (12 × 3 = 36),     and 
   18   is a factor     (18 × 2 = 36),     and 
   there is no positive integer less than   36   that has both factors. 


As a special case,   if either   m   or   n   is zero,   then the least common multiple is zero.



One way to calculate the least common multiple is to iterate all the multiples of   m,   until you find one that is also a multiple of   n.

If you already have   gcd   for greatest common divisor,   then this formula calculates   lcm.



 



lcm
⁡
(
m
,
n
)
=




|

m
×
n

|



gcd
⁡
(
m
,
n
)





{\displaystyle \operatorname {lcm} (m,n)={\frac {|m\times n|}{\operatorname {gcd} (m,n)}}}




One can also find   lcm   by merging the prime decompositions of both   m   and   n.



Related task
   greatest common divisor.


See also

   MathWorld entry:   Least Common Multiple.
   Wikipedia entry:   Least common multiple.


/* NetRexx */
options replace format comments java crossref symbols nobinary
 
numeric digits 3000
 
runSample(arg)
return
 
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
method lcm(m_, n_) public static
  L_ = m_ * n_ % gcd(m_, n_)
  return L_
 
-- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
-- Euclid's algorithm - iterative implementation
method gcd(m_, n_) public static
  loop while n_ > 0
    c_ = m_ // n_
    m_ = n_
    n_ = c_
    end
  return m_
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
  parse arg samples
  if samples = '' | samples = '.' then
    samples = '-6 14 =    42 |' -
               '3  4 =    12 |' -
              '18 12 =    36 |' -
               '2  0 =     0 |' -
               '0 85 =     0 |' -
              '12 18 =    36 |' -
               '5 12 =    60 |' -
              '12 22 =   132 |' -
               '7 31 =   217 |' -
             '117 18 =   234 |' -
              '38 46 =   874 |' -
           '18 12 -5 =   180 |' -
           '-5 18 12 =   180 |' - -- confirm that other permutations work
           '12 -5 18 =   180 |' -
        '18 12 -5 97 = 17460 |' -
              '30 42 =   210 |' -
              '30 42 =     . |' - -- 210; no verification requested
              '18 12'             -- 36
 
  loop while samples \= ''
    parse samples sample '|' samples
    loop while sample \= ''
      parse sample mnvals '=' chk sample
      if chk = '' then chk = '.'
      mv = mnvals.word(1)
      loop w_ = 2 to mnvals.words mnvals
        nv = mnvals.word(w_)
        mv = mv.abs
        nv = nv.abs
        mv = lcm(mv, nv)
        end w_
      lv = mv
      select case chk
        when '.' then state = ''
        when lv  then state = '(verified)'
        otherwise     state = '(failed)'
        end
      mnvals = mnvals.space(1, ',').changestr(',', ', ')
      say 'lcm of' mnvals.right(15.max(mnvals.length)) 'is' lv.right(5.max(lv.length)) state
      end
    end
 
  return
 