How to resolve the algorithm Fibonacci n-step number sequences step by step in the D programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fibonacci n-step number sequences step by step in the D programming language
Table of Contents
Problem Statement
These number series are an expansion of the ordinary Fibonacci sequence where: For small values of
n
{\displaystyle n}
, Greek numeric prefixes are sometimes used to individually name each series. Allied sequences can be generated where the initial values are changed:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fibonacci n-step number sequences step by step in the D programming language
Source code in the d programming language
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);
}
}
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);
}
}
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));
}
}
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));
}
}
You may also check:How to resolve the algorithm Boolean values step by step in the HicEst programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the DWScript programming language
You may also check:How to resolve the algorithm Ordered words step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Percolation/Mean run density step by step in the Racket programming language
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the J programming language