RosettaCodeData/Task/Least-common-multiple/NetRexx/least-common-multiple.netrexx
2023-07-01 13:44:08 -04:00

70 lines
2 KiB
Text

/* 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