Last active
September 5, 2019 03:45
-
-
Save rakin92/8b6af108d633e26e62228c72dadd4322 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
// FIBONACCI RECURSION O(2^N) | |
function fibonacciRecursion(num) { | |
if (num <= 1) return 1; | |
return fibonacci(num - 1) + fibonacci(num - 2); | |
} | |
// FIBONACCI LOOP O(N) | |
function fibonacciLoop(n) { | |
let a = 1, b = 0, temp; | |
while (num >= 0){ | |
temp = a; | |
a = a + b; | |
b = temp; | |
num -= 1; | |
} | |
return b; | |
} | |
// FIBONACCI MEMO O(N) | |
function fibonacciMemo(num, memo) { | |
memo = memo || {}; | |
if (memo[num]) return memo[num]; | |
if (num <= 1) return 1; | |
return memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment