Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
23
Task/Hailstone-sequence/D/hailstone-sequence-1.d
Normal file
23
Task/Hailstone-sequence/D/hailstone-sequence-1.d
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
auto hailstone(uint n) pure nothrow {
|
||||
auto result = [n];
|
||||
while (n != 1) {
|
||||
n = (n & 1) ? (n * 3 + 1) : (n / 2);
|
||||
result ~= n;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
immutable h = M.hailstone;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
immutable p = iota(1, N)
|
||||
.map!(i => tuple(i.hailstone.length, i))
|
||||
.reduce!max;
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
20
Task/Hailstone-sequence/D/hailstone-sequence-2.d
Normal file
20
Task/Hailstone-sequence/D/hailstone-sequence-2.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import std.stdio, std.algorithm, std.typecons, std.range;
|
||||
|
||||
auto hailstone(uint m) pure nothrow @nogc {
|
||||
return m
|
||||
.recurrence!q{ a[n - 1] & 1 ? a[n - 1] * 3 + 1 : a[n - 1]/2}
|
||||
.until!q{ a == 1 }(OpenRight.no);
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
immutable h = M.hailstone.array;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
immutable p = iota(1, N)
|
||||
.map!(i => tuple(i.hailstone.walkLength, i))
|
||||
.reduce!max;
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
23
Task/Hailstone-sequence/D/hailstone-sequence-3.d
Normal file
23
Task/Hailstone-sequence/D/hailstone-sequence-3.d
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
struct Hailstone {
|
||||
uint n;
|
||||
bool empty() const pure nothrow @nogc { return n == 0; }
|
||||
uint front() const pure nothrow @nogc { return n; }
|
||||
void popFront() pure nothrow @nogc {
|
||||
n = n == 1 ? 0 : (n & 1 ? (n * 3 + 1) : n / 2);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
enum M = 27;
|
||||
immutable h = M.Hailstone.array;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
immutable p = iota(1, N)
|
||||
.map!(i => tuple(i.Hailstone.walkLength, i))
|
||||
.reduce!max;
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
33
Task/Hailstone-sequence/D/hailstone-sequence-4.d
Normal file
33
Task/Hailstone-sequence/D/hailstone-sequence-4.d
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
struct Hailstone(size_t cacheSize = 500_000) {
|
||||
size_t n;
|
||||
__gshared static size_t[cacheSize] cache;
|
||||
|
||||
bool empty() const pure nothrow @nogc { return n == 0; }
|
||||
size_t front() const pure nothrow @nogc { return n; }
|
||||
|
||||
void popFront() nothrow {
|
||||
if (n >= cacheSize) {
|
||||
n = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2);
|
||||
} else if (cache[n]) {
|
||||
n = cache[n];
|
||||
} else {
|
||||
immutable n2 = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2);
|
||||
n = cache[n] = n2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
const h = M.Hailstone!().array;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
immutable p = iota(1, N)
|
||||
.map!(i => tuple(i.Hailstone!().walkLength, i))
|
||||
.reduce!max;
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
24
Task/Hailstone-sequence/D/hailstone-sequence-5.d
Normal file
24
Task/Hailstone-sequence/D/hailstone-sequence-5.d
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons, std.concurrency;
|
||||
|
||||
auto hailstone(size_t n) {
|
||||
return new Generator!size_t({
|
||||
yield(n);
|
||||
while (n > 1) {
|
||||
n = (n & 1) ? (3 * n + 1) : (n / 2);
|
||||
yield(n);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
const h = M.hailstone.array;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
immutable p = iota(1, N)
|
||||
.map!(i => tuple(i.hailstone.walkLength, i))
|
||||
.reduce!max;
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue