Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added Fibonacci Number with recursion - Dynamic Programming #1110

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

Closed
NitinRamnani wants to merge 1 commit into TheAlgorithms:master from NitinRamnani:master
Closed
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
26 changes: 26 additions & 0 deletions Dynamic-Programming/FibonacciNumberRecursive.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @function fibonacci
* @description Fibonacci is the sum of previous two fibonacci numbers.
* @param {Integer} N - The input integer
* @return {Integer} fibonacci of N.
* @see [Fibonacci_Numbers](https://en.wikipedia.org/wiki/Fibonacci_number)
*/
const fibonacci = (N) => {
if (!Number.isInteger(N)) {
throw new TypeError('Input should be integer')
}
const dp = new Map()
return fiboDP(N, dp)
}

const fiboDP = (N, dp) => {
if (N <= 1) return N

if (dp.has(N)) return dp.get(N)

const result = fiboDP(N - 1, dp) + fiboDP(N - 2, dp)
dp.set(N, result)
return result
}

export { fibonacci }
24 changes: 24 additions & 0 deletions Dynamic-Programming/tests/FibonacciNumberRecursive.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { fibonacci } from '../FibonacciNumberRecursive'

describe('Testing FibonacciNumberRecursive', () => {
it('fibonacci of 0', () => {
expect(fibonacci(0)).toBe(0)
})

it('fibonacci of 1', () => {
expect(fibonacci(1)).toBe(1)
})

it('fibonacci of 10', () => {
expect(fibonacci(10)).toBe(55)
})

it('fibonacci of 25', () => {
expect(fibonacci(25)).toBe(75025)
})

it('Testing for invalid type', () => {
expect(() => fibonacci('abc')).toThrowError()
expect(() => fibonacci()).toThrowError()
})
})

AltStyle によって変換されたページ (->オリジナル) /