Compare commits

...

6 Commits

Author SHA1 Message Date
JenilChristo
423cad2935
Merge 0c8060e92f into 2c67b48c21 2024-04-25 08:17:03 +08:00
JenilChristo
0c8060e92f
Merge branch 'master' into fibonacci 2019-08-06 11:59:18 +05:30
JenilChristo
44bf05da8e
Merge branch 'master' into fibonacci 2019-04-19 18:08:21 +05:30
JenilChristo
8a73675814
Merge branch 'master' into fibonacci 2019-01-01 09:24:07 +05:30
JenilChristo
d37f582a1c
Merge branch 'master' into fibonacci 2018-12-28 10:49:12 +05:30
jenilChristo
5088afd4ba Added optimized solution for fibonacciNth problem 2018-12-16 01:06:55 +05:30
3 changed files with 64 additions and 0 deletions

View File

@ -20,6 +20,17 @@ The Fibonacci spiral: an approximation of the golden spiral created by drawing c
![Fibonacci Spiral](https://upload.wikimedia.org/wikipedia/commons/2/2e/FibonacciSpiral.svg)
## Backtracking
Backtracking is a general algorithm for finding all (or some) solutions to some computational problem, that incrementally builds candidates to the solution.
## Memoization
In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
## Closures
A closure is a property of a function object by which it can access the variables outside the function or the parent scope.
## References
- [Wikipedia](https://en.wikipedia.org/wiki/Fibonacci_number)
- [Youtube] (https://www.youtube.com/watch?v=71AtaJpJHw0)
- [Wikipedia] (https://en.wikipedia.org/wiki/Memoization)

View File

@ -0,0 +1,17 @@
import getFibonacci from '../fibonacciNthBackTracking';
describe('fibonacciNthBackTracking', () => {
it('should return correct fibonnaci number ar a given position', () => {
expect(getFibonacci(1)).toBe(1);
expect(getFibonacci(2)).toBe(1);
expect(getFibonacci(3)).toBe(2);
expect(getFibonacci(4)).toBe(3);
expect(getFibonacci(5)).toBe(5);
expect(getFibonacci(75)).toBe(2111485077978050);
expect(getFibonacci(80)).toBe(23416728348467685);
expect(getFibonacci(90)).toBe(2880067194370816120);
expect(getFibonacci(101)).toBe(573147844013817200000);
expect(getFibonacci(500)).toBe(1.394232245616977e+104);
expect(getFibonacci(1400)).toBe(1.7108476902340223e+292);
});
});

View File

@ -0,0 +1,36 @@
/**
* Calculate fibonacci number at specific position using BackTracking approach
* and concepts of memoization/closures.
* The function doesn't backtrack values when it is cached in memoized
* array,otherwise it does backtracking and stores in memoized array.
* This way,execution of function query takes O(1) in best case and
* O(n) in worst case
*
* @param n
* @return {number}
*/
const fibonacciNth = () => {
// memoized array for storing calculated values,this acts as a closure for inner function
const memoized = [];
return (n) => {
// First two numbers in fibonacci series is 0 and 1
memoized[0] = 0;
memoized[1] = 1;
// When the memoized value is present return immediately in O(1)
if (memoized[n] !== undefined) return memoized[n];
// When not present backtrack, calculate, store the results for all elements < n in O(n)
let count;
for (count = 2; count <= n; count += 1) {
// for each number add the (count-1) and (count-2) and store in memoized array till we reach n
memoized[count] = memoized[count - 1] + memoized[count - 2];
}
// return the calculated result
return memoized[n];
};
};
const getFibonacci = fibonacciNth();
export default getFibonacci;