Skip to content

Instantly share code, notes, and snippets.

@mytholog
Last active November 29, 2016 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mytholog/7321ab88d98aaf2cb7b935b8577ecbf7 to your computer and use it in GitHub Desktop.
Save mytholog/7321ab88d98aaf2cb7b935b8577ecbf7 to your computer and use it in GitHub Desktop.
// O(2^N)
const fibonacci = function(n){
return n < 2 ? n : fibonacci(n-1) + fibonacci(n-2);
}
// to speed it up - O(2N)
function fibMemo(num, memo = {}) {
if (memo[num]) return memo[num];
if (num <= 1) return 1;
return memo[num] = fibMemo(num - 1, memo) + fibMemo(num - 2, memo);
}
// fastest - O(N)
function fibWhile(num){
var a = 1, b = 0, temp;
while (num >= 0){
temp = a;
a = a + b;
b = temp;
num--;
}
return b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment