Least common multiple: Difference between revisions
Add Cowgol
Not a robot (talk | contribs) (Add CLU) |
Not a robot (talk | contribs) (Add Cowgol) |
||
Line 1,043:
In this code, the <tt>lambda</tt> finds the least common multiple of two integers, and the <tt>reduce</tt> transforms it to accept any number of parameters. The <tt>reduce</tt> operation exploits how ''lcm'' is associative, <tt>(lcm a b c) == (lcm (lcm a b) c)</tt>; and how 1 is an identity, <tt>(lcm 1 a) == a</tt>.
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
sub gcd(m: uint32, n: uint32): (r: uint32) is
while n != 0 loop
var t := m;
m := n;
n := t % n;
end loop;
r := m;
end sub;
sub lcm(m: uint32, n: uint32): (r: uint32) is
if m==0 or n==0 then
r := 0;
else
r := m*n / gcd(m,n);
end if;
end sub;
print_i32(lcm(12, 18));
print_nl();</lang>
{{out}}
<pre>36</pre>
=={{header|D}}==
|