Advertisement
vit134

N-ое число Фибоначчи

Nov 15th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Вернуть n-ое число из последовательности Фибоначчи (0 1 1 2 3 5 8 ...)
  3. */
  4.  
  5. function fib(n) {
  6.     let results = [0,1];
  7.    
  8.     function find(n) {
  9.         if ( n == 0 ) return results[0];
  10.  
  11.         if (n == 1) return results[1];
  12.  
  13.         if (!results[n]){
  14.             results[n] = find(n-2) + find(n-1);
  15.         }
  16.         return results[n];
  17.     }
  18.    
  19.     return find(n);
  20. }
  21.  
  22. console.log(fib(6)) //8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement