Skip to content

Instantly share code, notes, and snippets.

@darkcris1
Created January 3, 2021 11:38
Show Gist options
  • Save darkcris1/d77f856cefca982c86349e937ce5f96f to your computer and use it in GitHub Desktop.
Save darkcris1/d77f856cefca982c86349e937ce5f96f to your computer and use it in GitHub Desktop.
n-bonacci algorithm | SoloLearn
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