SHARE
TWEET

Untitled

a guest Aug 21st, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fib (n, cache) {
  2.   cache = cache || {};
  3.  
  4.   if(n >= -1 && n <= 1) return BigInt(n);
  5.   let a, b;
  6.  
  7.   if(n > 0) {
  8.     a = cache[n-1] ? cache[n-1] : cache[n-1] = fib(n-1, cache);
  9.     b = cache[n-2] ? cache[n-2] : cache[n-2] = fib(n-2, cache);
  10.  
  11.   } else {
  12.     a = cache[n+1] ? cache[n+1] : cache[n+1] = fib(n+1, cache);
  13.     b = cache[n+2] ? cache[n+2] : cache[n+2] = fib(n+2, cache);  
  14.   }
  15.  
  16.   return BigInt(a + b);
  17. }
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
 
Top