Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save speckworks/dc930a23bdbaf1572dc089ebf735f102 to your computer and use it in GitHub Desktop.
Save speckworks/dc930a23bdbaf1572dc089ebf735f102 to your computer and use it in GitHub Desktop.
//This is a recursive Algorithm for generating the Fibonacci series. It will require O(n^2),
because it calls itself and has to allocate space for a new Array until it reaches the "break point",
which here is "number === 1".
const fibonacciSeq = (number) => {
if(number === 1) {
return [0,1];
} else {
let fib = fibonacciSeq(number - 1);
fib.push(fib[fib.length - 1] + fib[fib.length - 2]);
return fib;
}
}
fibonacciSeq(10);
//output is the array [
0, 1, 1, 2, 3,
5, 8, 13, 21, 34,
55
]
//note it had to create the array 10 times while creating the sequence
and pushing elements into it's array, for input of a single value 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment