tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
17
Task/Pythagorean-triples/D/pythagorean-triples-1.d
Normal file
17
Task/Pythagorean-triples/D/pythagorean-triples-1.d
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import std.stdio;
|
||||
|
||||
ulong[2] tri(ulong lim, ulong a=3, ulong b=4, ulong c=5) {
|
||||
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() {
|
||||
foreach (immutable p; 1 .. 8)
|
||||
writeln(10 ^^ p, " ", tri(10 ^^ p));
|
||||
}
|
||||
40
Task/Pythagorean-triples/D/pythagorean-triples-2.d
Normal file
40
Task/Pythagorean-triples/D/pythagorean-triples-2.d
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import std.stdio;
|
||||
|
||||
alias uint xuint; // ulong if going over 1 billion.
|
||||
|
||||
__gshared xuint nTriples, nPrimitives, limit;
|
||||
|
||||
void countTriples(xuint x, xuint y, xuint z) nothrow {
|
||||
while (true) {
|
||||
immutable xuint p = x + y + z;
|
||||
if (p > limit)
|
||||
return;
|
||||
|
||||
nPrimitives++;
|
||||
nTriples += limit / p;
|
||||
|
||||
xuint t0 = x - 2 * y + 2 * z;
|
||||
xuint t1 = 2 * x - y + 2 * z;
|
||||
xuint 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 (p; 1 .. 9) {
|
||||
limit = (cast(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