diff --git a/src/algorithms/math/fibonacci/README.md b/src/algorithms/math/fibonacci/README.md index 3d476c50..09ba5c89 100644 --- a/src/algorithms/math/fibonacci/README.md +++ b/src/algorithms/math/fibonacci/README.md @@ -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) diff --git a/src/algorithms/math/fibonacci/__test__/fibonacciNthBackTracking.test.js b/src/algorithms/math/fibonacci/__test__/fibonacciNthBackTracking.test.js new file mode 100644 index 00000000..cd065d08 --- /dev/null +++ b/src/algorithms/math/fibonacci/__test__/fibonacciNthBackTracking.test.js @@ -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); + }); +}); diff --git a/src/algorithms/math/fibonacci/fibonacciNthBackTracking.js b/src/algorithms/math/fibonacci/fibonacciNthBackTracking.js new file mode 100644 index 00000000..7f3bdfa4 --- /dev/null +++ b/src/algorithms/math/fibonacci/fibonacciNthBackTracking.js @@ -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;