Skip to content

Commit f4fdfd2

Browse files
Fibonacci without recursion added (#159)
* changes made * Update Fibonacci.js * fib Co-authored-by: vinayak <[email protected]>
1 parent 8655fd0 commit f4fdfd2

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

Algorithms/Dynamic-Programming/Fibonacci.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

Maths/Fibonacci.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ const FibonacciRecursiveDP = (stairs) => {
4848
return res
4949
}
5050

51+
// Algorithms
52+
// Calculates Fibonacci(n) such that Fibonacci(n) = Fibonacci(n - 1) + Fibonacci(n - 2)
53+
// Fibonacci(0) = Fibonacci(1) = 1
54+
// Uses a bottom up dynamic programming approach
55+
// Solve each sub-problem once, using results of previous sub-problems
56+
// which are n-1 and n-2 for Fibonacci numbers
57+
// Although this algorithm is linear in space and time as a function
58+
// of the input value n, it is exponential in the size of n as
59+
// a function of the number of input bits
60+
// @Satzyakiz
61+
62+
const FibonacciDpWithoutRecursion = (number) => {
63+
const table = []
64+
table.push(1)
65+
table.push(1)
66+
for (var i = 2; i < number; ++i) {
67+
table.push(table[i - 1] + table[i - 2])
68+
}
69+
return (table)
70+
}
71+
5172
// testing
5273

5374
console.log(FibonacciIterative(5))
@@ -57,3 +78,6 @@ console.log(FibonacciRecursive(5))
5778

5879
console.log(FibonacciRecursiveDP(5))
5980
// Output: 5
81+
82+
console.log(FibonacciDpWithoutRecursion(5))
83+
// Output: [ 1, 1, 2, 3, 5 ]

0 commit comments

Comments
 (0)