Pythagorean triples: Difference between revisions

Added D version
(Add 64bit only Modula-3)
(Added D version)
Line 167:
Up to 10000000: 9706567 triples, 702309 primitives.
Up to 100000000: 113236940 triples, 7023027 primitives.</lang>
 
=={{header|D}}==
{{trans|C}}
With the dmd compiler use -L/STACK:2000000 to increase stack size.
<lang d>import std.stdio;
 
// Should be ulong if going to or over 1 billion.
alias ulong xint;
__gshared xint total, prim;
 
void triples(const ref xint[3] IN, in xint maxPeri) nothrow {
static const xint[9][3] U = [[ 1, -2, 2, 2, -1, 2, 2, -2, 3],
[ 1, 2, 2, 2, 1, 2, 2, 2, 3],
[-1, 2, 2, -2, 1, 2, -2, 2, 3]];
const xint p = IN[0] + IN[1] + IN[2];
if (p > maxPeri)
return;
prim++;
 
// For every primitive triangle, its multiples would be
// right-angled too; count them up to the max perimeter.
total += maxPeri / p;
 
// recursively produce next tier by multiplying the matrices
foreach (i; 0 .. 3) {
xint[3] t = void;
t[0] = U[i][0] * IN[0] + U[i][1] * IN[1] + U[i][2] * IN[2];
t[1] = U[i][3] * IN[0] + U[i][4] * IN[1] + U[i][5] * IN[2];
t[2] = U[i][6] * IN[0] + U[i][7] * IN[1] + U[i][8] * IN[2];
triples(t, maxPeri);
}
}
 
void main() {
xint[3] seed = [3, 4, 5];
 
foreach (p; 1 .. 10) {
total = prim = 0;
triples(seed, 10 ^^ p);
writefln("Up to %10d: %10d triples, %8d primitives.",
10 ^^ p, total, prim);
}
}</lang>
Output:<pre>Up to 10: 0 triples, 0 primitives.
Up to 100: 17 triples, 7 primitives.
Up to 1000: 325 triples, 70 primitives.
Up to 10000: 4858 triples, 703 primitives.
Up to 100000: 64741 triples, 7026 primitives.
Up to 1000000: 808950 triples, 70229 primitives.
Up to 10000000: 9706567 triples, 702309 primitives.
Up to 100000000: 113236940 triples, 7023027 primitives.
Up to 1000000000: 1294080089 triples, 70230484 primitives.</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
Anonymous user