Skip to content

Instantly share code, notes, and snippets.

@kharioki
Created July 4, 2021 15:55
Show Gist options
  • Save kharioki/ef7c9c398348efaf9a3cebab9c5c9791 to your computer and use it in GitHub Desktop.
Save kharioki/ef7c9c398348efaf9a3cebab9c5c9791 to your computer and use it in GitHub Desktop.
Tribonacci - it works basically like a Fibonacci, but summing the last 3 (instead of 2) numbers of the sequence to generate the next.
function tribonacci(n) {
let seq = [1,1,1];
let index;
for (let i = 3; i < n; i++) {
seq.push(seq[seq.length - 1] + seq[seq.length - 2] + seq[seq.length - 3])
}
console.log(seq)
return seq.slice(0, n);
}
tribonacci(12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment