From 5510bb8274790c24a0daa3f94bb4d67e390d20eb Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Tue, 24 Sep 2019 00:05:18 +0530 Subject: [PATCH] update: Fibonacci using tabular method --- src/_Classics_/fibonacci/index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/_Classics_/fibonacci/index.js b/src/_Classics_/fibonacci/index.js index 936f01ae..e0852c5f 100644 --- a/src/_Classics_/fibonacci/index.js +++ b/src/_Classics_/fibonacci/index.js @@ -34,3 +34,24 @@ function fibonacciMemoized(index, cache) { } return cache[index]; } + +/** + * Using the bottom up approach, also known as tabular method + */ + +function fibonacciTabular(n) { + const table = [0, 1]; + + for (let i = 2; i <= n; i += 1) { + table[i] = table[i - 1] + table[i - 2]; + } + + return table[n]; +} + +// const number = 50; + +// console.log(`Fib normal - ${fibonacci(number)}`); +// console.log('--'); +// console.log(`Fib memo - ${fibonacciMemoized(number)}`); +// console.log(`Fib table - ${fibonacciTabular(number)}`);