Created
June 17, 2020 13:57
-
-
Save speckworks/dc930a23bdbaf1572dc089ebf735f102 to your computer and use it in GitHub Desktop.
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
//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