Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,29 @@
|
|||
void main() {
|
||||
import std.stdio, std.algorithm, std.range, std.conv;
|
||||
|
||||
const(int)[] memo;
|
||||
size_t addNum;
|
||||
|
||||
void setHead(int[] head) nothrow @safe {
|
||||
memo = head;
|
||||
addNum = head.length;
|
||||
}
|
||||
|
||||
int fibber(in size_t n) nothrow @safe {
|
||||
if (n >= memo.length)
|
||||
memo ~= iota(n - addNum, n).map!fibber.sum;
|
||||
return memo[n];
|
||||
}
|
||||
|
||||
setHead([1, 1]);
|
||||
10.iota.map!fibber.writeln;
|
||||
setHead([2, 1]);
|
||||
10.iota.map!fibber.writeln;
|
||||
|
||||
const prefixes = "fibo tribo tetra penta hexa hepta octo nona deca";
|
||||
foreach (immutable n, const name; prefixes.split.enumerate(2)) {
|
||||
setHead(1 ~ iota(n - 1).map!q{2 ^^ a}.array);
|
||||
writefln("n=%2d, %5snacci -> %(%d %) ...", n, name,
|
||||
15.iota.map!fibber);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
import std.stdio, std.algorithm, std.range, std.conv;
|
||||
|
||||
struct fiblike(T) {
|
||||
const(T)[] memo;
|
||||
immutable size_t addNum;
|
||||
|
||||
this(in T[] start) nothrow @safe {
|
||||
this.memo = start.dup;
|
||||
this.addNum = start.length;
|
||||
}
|
||||
|
||||
T opCall(in size_t n) nothrow @safe {
|
||||
if (n >= memo.length)
|
||||
memo ~= iota(n - addNum, n)
|
||||
.map!(i => opCall(i))
|
||||
.sum
|
||||
.to!int;
|
||||
return memo[n];
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto fibo = fiblike!int([1, 1]);
|
||||
iota(10).map!fibo.writeln;
|
||||
|
||||
auto lucas = fiblike!int([2, 1]);
|
||||
iota(10).map!lucas.writeln;
|
||||
|
||||
const prefixes = "fibo tribo tetra penta hexa hepta octo nona deca";
|
||||
foreach (immutable n, const name; prefixes.split.enumerate(2)) {
|
||||
auto fib = fiblike!int(1 ~ iota(n - 1).map!q{2 ^^ a}.array);
|
||||
writefln("n=%2d, %5snacci -> %(%d %) ...",
|
||||
n, name, 15.iota.map!fib);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
import std.stdio, std.algorithm, std.range, std.traits;
|
||||
|
||||
struct Fiblike(T) {
|
||||
T[] tail;
|
||||
|
||||
int opApply(int delegate(immutable ref T) dg) {
|
||||
int result, pos;
|
||||
foreach (immutable x; tail) {
|
||||
result = dg(x);
|
||||
if (result)
|
||||
return result;
|
||||
}
|
||||
foreach (immutable i; tail.length.iota.cycle) {
|
||||
immutable x = tail.sum;
|
||||
result = dg(x);
|
||||
if (result)
|
||||
break;
|
||||
tail[i] = x;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// std.range.take doesn't work with opApply.
|
||||
ForeachType!It[] takeApply(It)(It iterable, in size_t n) {
|
||||
typeof(return) result;
|
||||
foreach (immutable x; iterable) {
|
||||
result ~= x;
|
||||
if (result.length == n)
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
Fiblike!int([1, 1]).takeApply(10).writeln;
|
||||
Fiblike!int([2, 1]).takeApply(10).writeln;
|
||||
|
||||
const prefixes = "fibo tribo tetra penta hexa hepta octo nona deca";
|
||||
foreach (immutable n, const name; prefixes.split.enumerate(2)) {
|
||||
auto fib = Fiblike!int(1 ~ iota(n - 1).map!q{2 ^^ a}.array);
|
||||
writefln("n=%2d, %5snacci -> %s", n, name, fib.takeApply(15));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
void main() {
|
||||
import std.stdio, std.algorithm, std.range, std.concurrency;
|
||||
|
||||
immutable fibLike = (int[] tail) => new Generator!int({
|
||||
foreach (x; tail)
|
||||
yield(x);
|
||||
foreach (immutable i; tail.length.iota.cycle)
|
||||
yield(tail[i] = tail.sum);
|
||||
});
|
||||
|
||||
foreach (seed; [[1, 1], [2, 1]])
|
||||
fibLike(seed).take(10).writeln;
|
||||
|
||||
immutable prefixes = "fibo tribo tetra penta hexa hepta octo nona deca";
|
||||
foreach (immutable n, const name; prefixes.split.enumerate(2)) {
|
||||
auto fib = fibLike(1 ~ iota(n - 1).map!q{2 ^^ a}.array);
|
||||
writefln("n=%2d, %5snacci -> %(%s, %), ...", n, name, fib.take(15));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue