Pythagorean triples: Difference between revisions

Content added Content deleted
(Updated D code)
(Simpler D code)
Line 208: Line 208:
Runtime up to 10_000_000_000: about 72 seconds.
Runtime up to 10_000_000_000: about 72 seconds.


A shorter version:
A shorter slower version:
<lang d>import std.stdio, std.range, std.algorithm;
<lang d>import std.stdio, std.range, std.algorithm;


ulong[2] tri(in ulong lim, in ulong a=3, in ulong b=4, in ulong c=5) {
ulong[2] tri(ulong lim, ulong a=3, ulong b=4, ulong c=5) {
const ulong l = a + b + c;
const ulong l = a + b + c;
if (l > lim) return [0, 0];
if (l > lim) return [0, 0];
ulong[2] r = [1, lim / l];
ulong[2] r = [1, lim / l];
r[]+= tri(lim, a - 2*b + 2*c, 2*a - b + 2*c, 2*a - 2*b + 3*c)[];
r[] += tri(lim, a - 2*b + 2*c, 2*a - b + 2*c, 2*a - 2*b + 3*c)[];
r[]+= tri(lim, a + 2*b + 2*c, 2*a + b + 2*c, 2*a + 2*b + 3*c)[];
r[] += tri(lim, a + 2*b + 2*c, 2*a + b + 2*c, 2*a + 2*b + 3*c)[];
r[]+= tri(lim, -a + 2*b + 2*c, -2*a + b + 2*c, -2*a + 2*b + 3*c)[];
r[] += tri(lim, -a + 2*b + 2*c, -2*a + b + 2*c, -2*a + 2*b + 3*c)[];
return r;
return r;
}
}