dailycodebase

2 month data structures and algorithmic scripting challenge starting from 20th December 2018 - Coding is Fun! 💯💯 Do it everyday!! Also, Do give us a ⭐ if you liked the repository

View on GitHub

cover

Day 56 - Memoized Fibonacci

Ques) Write a program to find the Nth term in fibonacci series using recursion, and then optimize your solution using memoizaion.

Solution

JavaScript Implementation

Solution

function memoize (func) { const cache = {} return function (...args) { if (cache [args]) { return cache [args]; } const result = func.apply (this, args); cache [args] = result; return result; } } function slowFib (n) { if (n<2) return n; return slowFib (n-1) + slowFib (n-2); } const fib = memoize (slowFib);