SHARE
TWEET

Untitled

a guest Sep 16th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // FIBONACCI RECURSION O(2^N)
  2. function fibonacciRecursion(num) {
  3.   if (num <= 1) return 1;
  4.  
  5.   return fibonacci(num - 1) + fibonacci(num - 2);
  6. }
  7.  
  8. // FIBONACCI LOOP O(N)
  9. function fibonacciLoop(n) {
  10.     let a = 1, b = 0, temp;
  11.  
  12.     while (num >= 0){
  13.         temp = a;
  14.         a = a + b;
  15.         b = temp;
  16.         num -= 1;
  17.     }
  18.  
  19.     return b;
  20. }
  21.  
  22. // FIBONACCI MEMO O(N)
  23. function fibonacciMemo(num, memo) {
  24.   memo = memo || {};
  25.  
  26.   if (memo[num]) return memo[num];
  27.   if (num <= 1) return 1;
  28.  
  29.   return memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo);
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top