Skip to content

Commit e14a3ef

Browse files
committed
Fibonacci series using memoization
1 parent 18cf66c commit e14a3ef

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function fibonacci (n) {
2+
const memo = [undefined, 1, 1]; // Memoization
3+
4+
return (function fib (val) {
5+
if (memo[val] !== undefined) {
6+
return memo[val];
7+
}
8+
9+
memo[val] = fib(val - 1) + fib(val - 2);
10+
return memo[val];
11+
})(n);
12+
}
13+
14+
console.log(fibonacci(300));

0 commit comments

Comments
 (0)