How to resolve the algorithm Fibonacci n-step number sequences step by step in the Wren 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 Wren 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 Wren programming language
Source code in the wren programming language
import "/fmt" for Fmt
var fibN = Fn.new { |initial, numTerms|
var n = initial.count
if (n < 2 || numTerms < 0) Fiber.abort("Invalid argument(s).")
if (numTerms <= n) return initial.toList
var fibs = List.filled(numTerms, 0)
for (i in 0...n) fibs[i] = initial[i]
for (i in n...numTerms) {
var sum = 0
for (j in i-n...i) sum = sum + fibs[j]
fibs[i] = sum
}
return fibs
}
var names = [
"fibonacci", "tribonacci", "tetranacci", "pentanacci", "hexanacci",
"heptanacci", "octonacci", "nonanacci", "decanacci"
]
var initial = [1, 1, 2, 4, 8, 16, 32, 64, 128, 256]
System.print(" n name values")
var values = fibN.call([2, 1], 15)
Fmt.write("$2d $-10s", 2, "lucas")
Fmt.aprint(values, 4, 0, "")
for (i in 0..8) {
values = fibN.call(initial[0...i + 2], 15)
Fmt.write("$2d $-10s", i + 2, names[i])
Fmt.aprint(values, 4, 0, "")
}
You may also check:How to resolve the algorithm Pythagorean triples step by step in the Rust programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the F# programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the JavaScript programming language
You may also check:How to resolve the algorithm S-expressions step by step in the Pike programming language