Guest User

Untitled

a guest
Jan 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. cache = {};
  2. function cachedFibonacci(num) {
  3. if (num <= 1) {
  4. cache[num] = 1;
  5. return 1;
  6. } else if (cache[num]) {
  7. return cache[num];
  8. }
  9.  
  10. const result = cachedFibonacci(num - 1) + cachedFibonacci(num - 2);
  11. cache[num] = result;
  12. return result;
  13. }
  14.  
  15. function fibonacciSequence(max, seq = [], current = 0) {
  16. if (current <= max) {
  17. seq.push(cachedFibonacci(current));
  18. return fibonacciSequence(max, seq, current + 1);
  19. } else {
  20. return seq;
  21. }
  22. }
Add Comment
Please, Sign In to add comment