Skip to content

Commit 5510bb8

Browse files
committed
update: Fibonacci using tabular method
1 parent 602df1f commit 5510bb8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/_Classics_/fibonacci/index.js

+21
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,24 @@ function fibonacciMemoized(index, cache) {
3434
}
3535
return cache[index];
3636
}
37+
38+
/**
39+
* Using the bottom up approach, also known as tabular method
40+
*/
41+
42+
function fibonacciTabular(n) {
43+
const table = [0, 1];
44+
45+
for (let i = 2; i <= n; i += 1) {
46+
table[i] = table[i - 1] + table[i - 2];
47+
}
48+
49+
return table[n];
50+
}
51+
52+
// const number = 50;
53+
54+
// console.log(`Fib normal - ${fibonacci(number)}`);
55+
// console.log('--');
56+
// console.log(`Fib memo - ${fibonacciMemoized(number)}`);
57+
// console.log(`Fib table - ${fibonacciTabular(number)}`);

0 commit comments

Comments
 (0)