RosettaCodeData/Task/Least-common-multiple/D/least-common-multiple.d

23 lines
461 B
D
Raw Permalink Normal View History

2014-01-17 05:32:22 +00:00
import std.stdio, std.bigint, std.math;
2013-04-10 21:29:02 -07:00
2015-02-20 00:35:01 -05:00
T gcd(T)(T a, T b) pure nothrow {
2014-01-17 05:32:22 +00:00
while (b) {
immutable t = b;
2013-04-10 21:29:02 -07:00
b = a % b;
a = t;
}
return a;
}
2015-02-20 00:35:01 -05:00
T lcm(T)(T m, T n) pure nothrow {
2014-01-17 05:32:22 +00:00
if (m == 0) return m;
if (n == 0) return n;
return abs((m * n) / gcd(m, n));
}
2013-04-10 21:29:02 -07:00
void main() {
2014-01-17 05:32:22 +00:00
lcm(12, 18).writeln;
lcm("2562047788015215500854906332309589561".BigInt,
"6795454494268282920431565661684282819".BigInt).writeln;
2013-04-10 21:29:02 -07:00
}