Skip to content

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
wants to merge 1 commit into from
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
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
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()
})
})
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy