Last active
November 29, 2016 04:40
-
-
Save mytholog/7321ab88d98aaf2cb7b935b8577ecbf7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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