Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
auto hailstone(int n) pure nothrow {
|
||||
auto hailstone(uint n) pure nothrow {
|
||||
auto result = [n];
|
||||
while (n != 1) {
|
||||
n = n & 1 ? n*3 + 1 : n/2;
|
||||
|
|
@ -11,12 +11,13 @@ auto hailstone(int n) pure nothrow {
|
|||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
auto h = hailstone(M);
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$-4 .. $]);
|
||||
writeln("length hailstone(", M, ")= ", h.length);
|
||||
immutable h = M.hailstone;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
auto s = iota(1, N).map!(i => tuple(hailstone(i).length, i))();
|
||||
auto p = reduce!max(s);
|
||||
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]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +1,23 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
struct Hail {
|
||||
int n;
|
||||
bool empty() { return n == 0; }
|
||||
int front() { return n; }
|
||||
void popFront() { n = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2); }
|
||||
struct Hailstone {
|
||||
uint n;
|
||||
bool empty() const pure nothrow { return n == 0; }
|
||||
uint front() const pure nothrow { return n; }
|
||||
void popFront() pure nothrow {
|
||||
n = n == 1 ? 0 : (n & 1 ? n*3 + 1 : n/2);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
auto h = array(Hail(M));
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$-4 .. $]);
|
||||
writeln("length hailstone(", M, ")= ", h.length);
|
||||
immutable h = M.Hailstone.array;
|
||||
writeln("hailstone(", M, ")= ", h[0 .. 4], " ... " , h[$ - 4 .. $]);
|
||||
writeln("Length hailstone(", M, ")= ", h.length);
|
||||
|
||||
enum N = 100_000;
|
||||
auto s = map!(i => tuple(walkLength(Hail(i)), i))(iota(1, N));
|
||||
auto p = reduce!max(s);
|
||||
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-3.d
Normal file
33
Task/Hailstone-sequence/D/hailstone-sequence-3.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 { return n == 0; }
|
||||
size_t front() const pure nothrow { 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]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue