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> |
|||
⚫ | |||
⚫ | |||
[2, 1, 2, 3, 5, 8, 13, 21, 34, 55] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
===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> |
||
⚫ | |||
⚫ | |||
2 1 3 4 7 11 18 29 47 76 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|Python}}== |
=={{header|Python}}== |