Skip to content

Fibonacci without recursion added #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions Algorithms/Dynamic-Programming/Fibonacci.js

This file was deleted.

24 changes: 24 additions & 0 deletions Maths/Fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -57,3 +78,6 @@ console.log(FibonacciRecursive(5))

console.log(FibonacciRecursiveDP(5))
// Output: 5

console.log(FibonacciDpWithoutRecursion(5))
// Output: [ 1, 1, 2, 3, 5 ]