diff --git a/Algorithms/Dynamic-Programming/Fibonacci.js b/Algorithms/Dynamic-Programming/Fibonacci.js deleted file mode 100644 index dce3bc63b4..0000000000 --- a/Algorithms/Dynamic-Programming/Fibonacci.js +++ /dev/null @@ -1,25 +0,0 @@ -// Calculates fib(n) such that fib(n) = fib(n - 1) + fib(n - 2) -// fib(0) = fib(1) = 1 -// Uses a bottom up dynamic programming approach -// Solve each subproblem once, using results of previous subproblems, -// which are n-1 and n-2 for Fibonacci numbers - -// Although this algorithm is linear in space and time as a function -// of the input value n, it is exponential in the size of n as -// a function of the number of input bits - -function fib (n) { - var table = [] - table.push(1) - table.push(1) - for (var i = 2; i < n; ++i) { - table.push(table[i - 1] + table[i - 2]) - } - console.log('Fibonacci #%d = %d', n, table[n - 1]) -} - -fib(1) -fib(2) -fib(200) -fib(5) -fib(10) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 3f8be74322..9a4361781d 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -48,6 +48,27 @@ const FibonacciRecursiveDP = (stairs) => { return res } +// Algorithms +// Calculates Fibonacci(n) such that Fibonacci(n) = Fibonacci(n - 1) + Fibonacci(n - 2) +// Fibonacci(0) = Fibonacci(1) = 1 +// Uses a bottom up dynamic programming approach +// Solve each sub-problem once, using results of previous sub-problems +// which are n-1 and n-2 for Fibonacci numbers +// Although this algorithm is linear in space and time as a function +// of the input value n, it is exponential in the size of n as +// a function of the number of input bits +// @Satzyakiz + +const FibonacciDpWithoutRecursion = (number) => { + const table = [] + table.push(1) + table.push(1) + for (var i = 2; i < number; ++i) { + table.push(table[i - 1] + table[i - 2]) + } + return (table) +} + // testing console.log(FibonacciIterative(5)) @@ -57,3 +78,6 @@ console.log(FibonacciRecursive(5)) console.log(FibonacciRecursiveDP(5)) // Output: 5 + +console.log(FibonacciDpWithoutRecursion(5)) +// Output: [ 1, 1, 2, 3, 5 ]