Created
January 3, 2021 11:38
-
-
Save darkcris1/d77f856cefca982c86349e937ce5f96f to your computer and use it in GitHub Desktop.
n-bonacci algorithm | SoloLearn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nFibonacci = (n, max) => { | |
const result = Array(n) | |
.fill(0) | |
.fill(1, n - 1 || 1) | |
for (let i = result.length; i < max; i++) { | |
const nSums = result | |
.slice(result.length - n) | |
.reduce((acc, val) => BigInt(acc) + BigInt(val), 0) | |
result.push(nSums) | |
} | |
return n < max ? result : result.slice(0, max) | |
} | |
console.log(nFibonacci(2, 10)) // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] | |
// N-bonacci visualizer | |
// https://code.sololearn.com/WGzh210dVDgZ/# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment