Pythagorean triples: Difference between revisions
Content added Content deleted
(added Fortran) |
(Improved D code) |
||
Line 177: | Line 177: | ||
__gshared xint total, prim; |
__gshared xint total, prim; |
||
void triples(in xint |
void triples(in xint maxPeri, |
||
in xint |
in xint in0=3, in xint in1=4, in xint in2=5) nothrow { |
||
static enum xint[9][3] U = [[ 1, -2, 2, 2, -1, 2, 2, -2, 3], |
static enum 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], |
||
Line 193: | Line 193: | ||
// recursively produce next tier by multiplying the matrices |
// recursively produce next tier by multiplying the matrices |
||
foreach (i; TypeTuple!(0, 1, 2)) |
foreach (i; TypeTuple!(0, 1, 2)) |
||
triples( |
triples(maxPeri, |
||
U[i][0] * in0 + U[i][1] * in1 + U[i][2] * in2, |
|||
U[i][3] * in0 + U[i][4] * in1 + U[i][5] * in2, |
U[i][3] * in0 + U[i][4] * in1 + U[i][5] * in2, |
||
U[i][6] * in0 + U[i][7] * in1 + U[i][8] * in2 |
U[i][6] * in0 + U[i][7] * in1 + U[i][8] * in2); |
||
maxPeri); |
|||
} |
} |
||
Line 202: | Line 202: | ||
foreach (p; 1 .. 10) { |
foreach (p; 1 .. 10) { |
||
total = prim = 0; |
total = prim = 0; |
||
triples( |
triples(10 ^^ p); |
||
writefln("Up to %10d: %10d triples, %8d primitives.", |
writefln("Up to %10d: %10d triples, %8d primitives.", |
||
10 ^^ p, total, prim); |
10 ^^ p, total, prim); |