Generalizations of Fibonacci numbers
Fibonacci numbers of higher order
A Fibonacci sequence of order \$n\$ is an integer sequence in which each sequence element is the sum of the previous \$n\$ elements (with the exception of the first \$n\$ elements in the sequence).
The usual Fibonacci numbers are a Fibonacci sequence of order \$2\$. The cases \$n = 3\$ and \$n = 4\$ have been thoroughly investigated. The number of compositions of nonnegative integers into parts that are at most \$n\$ is a Fibonacci sequence of order \$n\$. The sequence of the number of strings of \$0\$s and \$1\$s of length \$m\$ that contain at most \$n\$ consecutive 0s is also a Fibonacci sequence of order \$n\$.Tribonacci numbers
The tribonacci numbers are like the Fibonacci numbers, but instead of starting with two predetermined terms, the sequence starts with three predetermined terms and each term afterwards is the sum of the preceding three terms. The first few tribonacci numbers are:
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274, 504, 927, 1705, 3136, 5768, 10609, 19513, 35890, 66012, …
(sequence A000073 in the OEIS)
Tetranacci numbers
The tetranacci numbers start with four predetermined terms, each term afterwards being the sum of the preceding four terms. The first few tetranacci numbers are:
0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, 2872, 5536, 10671, 20569, 39648, 76424, 147312, 283953, 547337, …
(sequence A000078 in the OEIS)
The function is simple, you give it two arguments, the first argument is an array of numbers, the second argument is the number of terms.
The length of the array is the order of the generated sequence, the elements of the array are the first order terms of the generated sequence, the function uses iterative approach and generates the required sequence.
Examples:
> console.log(fiblike([0, 1], 16))
[
0, 1, 1, 2, 3, 5,
8, 13, 21, 34, 55, 89,
144, 233, 377, 610
]
undefined
> console.log(fiblike([0, 0, 1], 16))
[
0, 0, 1, 1, 2,
4, 7, 13, 24, 44,
81, 149, 274, 504, 927,
1705
]
undefined
> console.log(fiblike([0, 0, 0, 1], 16))
[
0, 0, 0, 1, 1,
2, 4, 8, 15, 29,
56, 108, 208, 401, 773,
1490
]
undefined
> console.log(fiblike([1, 2, 3], 16))
[
1, 2, 3, 6, 11,
20, 37, 68, 125, 230,
423, 778, 1431, 2632, 4841,
8904
]
undefined
> console.log(fiblike([9, 21, 1986], 16))
[
9, 21, 1986,
2016, 4023, 8025,
14064, 26112, 48201,
88377, 162690, 299268,
550335, 1012293, 1861896,
3424524
]
undefined
>
Code
function fiblike (array, terms) {
if (isNaN(terms) || !(array instanceof Array)) {
throw 'Arguments have incorrect types'
}
if (array.some(isNaN)) {
throw 'Array should only contain numbers'
}
var result = []
for (let i=0; i<terms; i++) {
array.push(array.reduce((s, n) => s + n, 0))
let first = array.shift()
result.push(first)
}
return result
}
console.log(fiblike([0, 1], 16))
console.log(fiblike([0, 0, 1], 16))
console.log(fiblike([0, 0, 0, 1], 16))
console.log(fiblike([1, 2, 3], 16))
console.log(fiblike([9, 21, 1986], 16))
The last example is the birth date of my favorite Youtuber and I am not gonna tell you her name
So how is the code? How can it be improved?
P.S. I really don't know why 1 instanceof Number
fails.
throw
exceptions. \$\endgroup\$1 instanceof Number
=> Literal numbers are not objects in Javascript. So they have no prototype property -instanceof
looks at an object'sprototype
property. As a practical matterNumber
is a wrapper for, what we call in other languages, "static" methods. Following is alsofalse
=>let one = new Number("1"); one.valueOf() instanceof Number;
\$\endgroup\$