Heronian triangles: Difference between revisions

Better D entry
(Promote from draft to full task status.)
(Better D entry)
Line 51:
enum uint maxSide = 200;
 
// BySort by increasing area, perimeter, then sides.
//auto h = cartesianProduct!3(iota(1, maxSide + 1))
auto r = iota(1, maxSide + 1);
autoconst h = cartesianProduct(r, r, r)
//.filter!({a, b, c} => ...
.filter!(t => t[0] <= t[1] && t[1] <= t[2] &&
t[0] + t[1] > t[2] &&
t[].gcd3 == 1 && t[].isHeronian)
.array;
h .schwartzSort!(t => tuple(t[].hero, t[].only.sum, t.reverse));
.release;
 
static void showTriangles(R)(R ts) @safe {
// By increasing area, perimeter, then sides.
"Area Perimeter Sides".writeln;
h.schwartzSort!(t => tuple(t[].hero, t[].only.sum, t.reverse));
foreach (immutable t; h.take(10)ts)
writefln(fmt"%3s %8d %3dx%dx%d", t[].hero, t[].only.sum, t[]);
}
 
writefln("Primitive Heronian triangles with sides up to %d: %d", maxSide, h.length);
"\nFirst ten when ordered by increasing area, then perimeter,then maximum sides:".writeln;
showTriangles(h.take(10));
"Area Perimeter Sides".writeln;
immutable fmt = "%3s %8d %3dx%dx%d";
foreach (immutable t; h.take(10))
writefln(fmt, t[].hero, t[].only.sum, t[]);
 
"\nAll with area 210 subject to the previous ordering:".writeln;
foreach showTriangles(immutable t; h.filter!(t => t[].hero == 210));
"Area Perimeter Sides".writeln;
foreach (immutable t; h.filter!(t => t[].hero == 210))
writefln(fmt, t[].hero, t[].only.sum, t[]);
}</lang>
{{out}}