Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cache = {};
- function cachedFibonacci(num) {
- if (num <= 1) {
- cache[num] = 1;
- return 1;
- } else if (cache[num]) {
- return cache[num];
- }
- const result = cachedFibonacci(num - 1) + cachedFibonacci(num - 2);
- cache[num] = result;
- return result;
- }
- function fibonacciSequence(max, seq = [], current = 0) {
- if (current <= max) {
- seq.push(cachedFibonacci(current));
- return fibonacciSequence(max, seq, current + 1);
- } else {
- return seq;
- }
- }
Add Comment
Please, Sign In to add comment