Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
15
Task/Pythagorean-triples/D/pythagorean-triples-1.d
Normal file
15
Task/Pythagorean-triples/D/pythagorean-triples-1.d
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
void main() @safe {
|
||||
import std.stdio, std.range, std.algorithm, std.typecons, std.numeric;
|
||||
|
||||
enum triples = (in uint n) pure nothrow @safe /*@nogc*/ =>
|
||||
iota(1, n + 1)
|
||||
.map!(z => iota(1, z + 1)
|
||||
.map!(x => iota(x, z + 1).map!(y => tuple(x, y, z))))
|
||||
.joiner.joiner
|
||||
.filter!(t => t[0] ^^ 2 + t[1] ^^ 2 == t[2] ^^ 2 && t[].only.sum <= n)
|
||||
.map!(t => tuple(t[0 .. 2].gcd == 1, t[]));
|
||||
|
||||
auto xs = triples(100);
|
||||
writeln("Up to 100 there are ", xs.count, " triples, ",
|
||||
xs.filter!q{ a[0] }.count, " are primitive.");
|
||||
}
|
||||
17
Task/Pythagorean-triples/D/pythagorean-triples-2.d
Normal file
17
Task/Pythagorean-triples/D/pythagorean-triples-2.d
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
ulong[2] tri(ulong lim, ulong a=3, ulong b=4, ulong c=5)
|
||||
pure nothrow @safe @nogc {
|
||||
immutable l = a + b + c;
|
||||
if (l > lim)
|
||||
return [0, 0];
|
||||
typeof(return) 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)[];
|
||||
return r;
|
||||
}
|
||||
|
||||
void main() /*@safe*/ {
|
||||
import std.stdio;
|
||||
foreach (immutable p; 1 .. 9)
|
||||
writeln(10 ^^ p, ' ', tri(10 ^^ p));
|
||||
}
|
||||
18
Task/Pythagorean-triples/D/pythagorean-triples-3.d
Normal file
18
Task/Pythagorean-triples/D/pythagorean-triples-3.d
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import std.stdio, core.simd;
|
||||
|
||||
ulong2 tri(in ulong lim, in ulong a=3, in ulong b=4, in ulong c=5)
|
||||
pure nothrow @safe @nogc {
|
||||
immutable l = a + b + c;
|
||||
if (l > lim)
|
||||
return [0, 0];
|
||||
typeof(return) 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);
|
||||
return r;
|
||||
}
|
||||
|
||||
void main() /*@safe*/ {
|
||||
foreach (immutable p; 1 .. 9)
|
||||
writeln(10 ^^ p, ' ', tri(10 ^^ p).array);
|
||||
}
|
||||
40
Task/Pythagorean-triples/D/pythagorean-triples-4.d
Normal file
40
Task/Pythagorean-triples/D/pythagorean-triples-4.d
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import std.stdio;
|
||||
|
||||
alias Xuint = uint; // ulong if going over 1 billion.
|
||||
|
||||
__gshared Xuint nTriples, nPrimitives, limit;
|
||||
|
||||
void countTriples(Xuint x, Xuint y, Xuint z) nothrow @nogc {
|
||||
while (true) {
|
||||
immutable p = x + y + z;
|
||||
if (p > limit)
|
||||
return;
|
||||
|
||||
nPrimitives++;
|
||||
nTriples += limit / p;
|
||||
|
||||
auto t0 = x - 2 * y + 2 * z;
|
||||
auto t1 = 2 * x - y + 2 * z;
|
||||
auto t2 = t1 - y + z;
|
||||
countTriples(t0, t1, t2);
|
||||
|
||||
t0 += 4 * y;
|
||||
t1 += 2 * y;
|
||||
t2 += 4 * y;
|
||||
countTriples(t0, t1, t2);
|
||||
|
||||
z = t2 - 4 * x;
|
||||
y = t1 - 4 * x;
|
||||
x = t0 - 2 * x;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (immutable p; 1 .. 9) {
|
||||
limit = Xuint(10) ^^ p;
|
||||
nTriples = nPrimitives = 0;
|
||||
countTriples(3, 4, 5);
|
||||
writefln("Up to %11d: %11d triples, %9d primitives.",
|
||||
limit, nTriples, nPrimitives);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue