A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
22
Task/Hailstone-sequence/D/hailstone-sequence-1.d
Normal file
22
Task/Hailstone-sequence/D/hailstone-sequence-1.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import std.stdio, std.algorithm, std.range, std.typecons;
|
||||
|
||||
auto hailstone(int 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;
|
||||
auto h = hailstone(M);
|
||||
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);
|
||||
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.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); }
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum M = 27;
|
||||
auto h = array(Hail(M));
|
||||
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);
|
||||
writeln("Longest sequence in [1,", N, "]= ",p[1]," with len ",p[0]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue