A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
17
Task/Hofstadter-Q-sequence/D/hofstadter-q-sequence-1.d
Normal file
17
Task/Hofstadter-Q-sequence/D/hofstadter-q-sequence-1.d
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import std.stdio, std.algorithm, std.functional, std.range;
|
||||
|
||||
int Q(int n) {
|
||||
assert(n > 0);
|
||||
alias memoize!Q mQ;
|
||||
if (n == 1 || n == 2)
|
||||
return 1;
|
||||
else
|
||||
return mQ(n - mQ(n - 1)) + mQ(n - mQ(n - 2));
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln("Q(n) for n = [1..10] is: ", map!Q(iota(1, 11)));
|
||||
writeln("Q(1000) = ", Q(1000));
|
||||
writefln("Q(i) is less than Q(i-1) for i [2..100_000] %d times.",
|
||||
count!(i => Q(i) < Q(i-1))(iota(2, 100_001)));
|
||||
}
|
||||
23
Task/Hofstadter-Q-sequence/D/hofstadter-q-sequence-2.d
Normal file
23
Task/Hofstadter-Q-sequence/D/hofstadter-q-sequence-2.d
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import std.stdio, std.algorithm, std.range, std.array;
|
||||
|
||||
struct Q {
|
||||
static Appender!(uint[]) s;
|
||||
|
||||
/*nothrow*/ static this() {
|
||||
s ~= [0, 1, 1];
|
||||
}
|
||||
|
||||
static uint opCall(in int n) /*nothrow*/ {
|
||||
assert(n > 0);
|
||||
foreach (immutable i; s.data.length .. n + 1)
|
||||
s ~= s.data[i - s.data[i - 1]] + s.data[i - s.data[i - 2]];
|
||||
return s.data[n];
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln("Q(n) for n = [1..10] is: ", map!Q(iota(1, 11)));
|
||||
writeln("Q(1000) = ", Q(1000));
|
||||
writefln("Q(i) is less than Q(i-1) for i [2..100_000] %d times.",
|
||||
count!(i => Q(i) < Q(i-1))(iota(2, 100_001)));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue