Jump to content

Least common multiple: Difference between revisions

→‎{{header|D}}: keep gcd separate so it can be reused; also allow bigint to return zero
(→‎{{header|D}}: keep gcd separate so it can be reused; also allow bigint to return zero)
Line 182:
<lang d>import std.stdio, std.bigint;
 
T lcm(T)(T m, T n) {
whileif (n2m !== 0) {return m;
in {
assertif (mn !== 0) &&return n != 0);
T resultr = (m * n) / m2gcd(m, n);
} body {
Treturn m2(r >= m0) ? r : -r;
}
T n2 = n;
while (n2 != 0) {
T t2 = m2;
m2 = n2;
n2 = t2 % n2;
}
 
T gcd(T)(T a, T b) {
T result = (m * n) / m2;
returnwhile (resultb >!= 0) ? result : -result;{
Tauto t2t = m2b;
m2b = n2a % b;
T n2 a = nt;
}
return a;
}
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.