Skip to content

Instantly share code, notes, and snippets.

@rakin92
Last active September 5, 2019 03:45
Show Gist options
  • Save rakin92/8b6af108d633e26e62228c72dadd4322 to your computer and use it in GitHub Desktop.
Save rakin92/8b6af108d633e26e62228c72dadd4322 to your computer and use it in GitHub Desktop.
// 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