Jump to content

Hofstadter Q sequence: Difference between revisions

Updated second D entry
m ({{out}})
(Updated second D entry)
Line 448:
<lang d>import std.stdio, std.algorithm, std.range, std.array;
 
static uint opCallQ(in int n) nothrow
struct Q {
in {
__gshared static Appender!(uint[]) s;
assert(n > 0);
} body {
__gshared static Appender!(uintint[]) s = [0, 1, 1];
 
foreach (immutable i; s.data.length .. n + 1)
nothrow static this() {
s ~= s.data[0,i 1,- s.data[i - 1]] + s.data[i - s.data[i - 2]];
return s.data[n];
}
 
static uint opCall(in int n) nothrow
in {
assert(n > 0);
} body {
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];
}
}
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.