Fibonacci n-step number sequences: Difference between revisions

Content deleted Content added
+ two D versions
Removed first D entry
Line 46: Line 46:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.range, std.conv, std.functional;

void main() {
int[] head;
int fibber(in size_t n) /*nothrow*/ {
alias memoize!fibber f;
if (n < head.length)
return head[n];
return iota(n - head.length, n).map!f().reduce!q{a + b}();
}

head = [1, 1];
iota(10).map!fibber().writeln();
head = [2, 1];
iota(10).map!fibber().writeln();

auto prefixes = "fibo tribo tetra penta hexa hepta octo nona deca";
foreach (n, name; zip(iota(2, 11), prefixes.split())) {
head = [1] ~ iota(n-1).map!q{2 ^^ a}().array();
const items = iota(15).map!(i => text(fibber(i)))().join(" ");
writefln("n=%2d, %5snacci -> %s ...", n, name, items);
}
}</lang>
{{out}}
<pre>[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
[2, 1, 2, 3, 5, 8, 13, 21, 34, 55]
n= 2, fibonacci -> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ...
n= 3, tribonacci -> 1 1 2 4 6 10 16 26 42 68 110 178 288 466 754 ...
n= 4, tetranacci -> 1 1 2 4 7 11 18 29 47 76 123 199 322 521 843 ...
n= 5, pentanacci -> 1 1 2 4 8 12 19 31 50 81 131 212 343 555 898 ...
n= 6, hexanacci -> 1 1 2 4 8 16 20 32 52 84 136 220 356 576 932 ...
n= 7, heptanacci -> 1 1 2 4 8 16 32 33 53 86 139 225 364 589 953 ...
n= 8, octonacci -> 1 1 2 4 8 16 32 64 54 87 141 228 369 597 966 ...
n= 9, nonanacci -> 1 1 2 4 8 16 32 64 128 88 142 230 372 602 974 ...
n=10, decanacci -> 1 1 2 4 8 16 32 64 128 256 143 231 374 605 979 ...</pre>
===Alternative Version===
The output is similar.
<lang d>import std.stdio, std.algorithm, std.range, std.conv;
<lang d>import std.stdio, std.algorithm, std.range, std.conv;


Line 131: Line 94:
}
}
}</lang>
}</lang>
{{out}}
<pre>1 1 2 3 5 8 13 21 34 55
2 1 3 4 7 11 18 29 47 76
n= 2, fibonacci -> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ...
n= 3, tribonacci -> 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ...
n= 4, tetranacci -> 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 ...
n= 5, pentanacci -> 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 ...
n= 6, hexanacci -> 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 ...
n= 7, heptanacci -> 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 ...
n= 8, octonacci -> 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 ...
n= 9, nonanacci -> 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ...
n=10, decanacci -> 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ...</pre>


=={{header|Python}}==
=={{header|Python}}==