From 4de64ad66a49d90f6640b738049e092d70133c92 Mon Sep 17 00:00:00 2001 From: Mateus Oliveira Date: Wed, 12 Dec 2018 17:42:24 +0000 Subject: [PATCH 1/4] Add recursive Fibonacci function using memoization --- .../fibonacci/fibonacciRecursiveMemoized.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js diff --git a/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js b/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js new file mode 100644 index 00000000..717245c2 --- /dev/null +++ b/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js @@ -0,0 +1,19 @@ +//memoize function use a Map to store function arguments and results, which are returned if found inside cache +const memoize = fn => { + const cache = new Map(); + + return num => { + if (cache.has(num)) return cache.get(num); + + const result = fn(num); + cache.set(num, result); + return result; + }; +}; + +//fibonacci function decorated with the memoize function, every call will first pass through the cache before it's computed +const fibonacci = memoize(n => { + if (n === 0 || n === 1) return n; + + return fibonacci(n - 1) + fibonacci(n - 2); +}); From a4c37d88d2a0d8b2265be5c45d46a2747f38b584 Mon Sep 17 00:00:00 2001 From: Mateus Oliveira Date: Wed, 12 Dec 2018 18:27:03 +0000 Subject: [PATCH 2/4] Add README-recursive.md explaining code There's already a README.md file in the folder so I couldn't come up with a better name, this is of course subject to change. --- .../math/fibonacci/README-recursive.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/algorithms/math/fibonacci/README-recursive.md diff --git a/src/algorithms/math/fibonacci/README-recursive.md b/src/algorithms/math/fibonacci/README-recursive.md new file mode 100644 index 00000000..37a432c7 --- /dev/null +++ b/src/algorithms/math/fibonacci/README-recursive.md @@ -0,0 +1,19 @@ +# Fibonacci recursive function using memoization as a decorator + +The Fibonacci algorithm in its recursive form may be considered cursed by the majority of developers (because it is), but there is a way to make it work: using memoization. The memoize function is a closure and needs another function as an argument, then its result is checked on the Map object; if it's there, return it, if not, add it to the Map. As the algorithm grows in size, it will only return references rather than computing values over and over again. + +## 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. Memoization has also been used in other contexts (and for purposes other than speed gains), such as in simple mutually recursive descent parsing. Although related to caching, memoization refers to a specific case of this optimization, distinguishing it from forms of caching such as buffering or page replacement. In the context of some logic programming languages, memoization is also known as tabling. + +## Recursion + +Recursion in computer science is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem (as opposed to iteration).[1] The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science. + +## Closure +More info on closures here: https://javascript.info/closure + +## References + - [Wikipedia](https://en.wikipedia.org/wiki/Memoization) + - [Wikipedia](https://en.wikipedia.org/wiki/Recursion_(computer_science)) + - [JavaScript.info](https://javascript.info/closure) From 0bc45ea5a5396d14cab6c857baa5553dbbeead97 Mon Sep 17 00:00:00 2001 From: Mateus Oliveira Date: Wed, 12 Dec 2018 18:41:35 +0000 Subject: [PATCH 3/4] minor formatting changes --- .../math/fibonacci/fibonacciRecursiveMemoized.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js b/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js index 717245c2..bc6a4e32 100644 --- a/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js +++ b/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js @@ -1,8 +1,9 @@ -//memoize function use a Map to store function arguments and results, which are returned if found inside cache -const memoize = fn => { +/* memoize function use a Map to store function arguments + and results, which are returned if found inside cache */ +const memoize = (fn) => { const cache = new Map(); - return num => { + return (num) => { if (cache.has(num)) return cache.get(num); const result = fn(num); @@ -11,8 +12,9 @@ const memoize = fn => { }; }; -//fibonacci function decorated with the memoize function, every call will first pass through the cache before it's computed -const fibonacci = memoize(n => { +/* fibonacci function decorated with the memoize function, every call + will first pass through the cache before it's computed */ +const fibonacci = memoize((n) => { if (n === 0 || n === 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); From 7cb08f873af979712013fd27ea558c98c28b6ec6 Mon Sep 17 00:00:00 2001 From: Mateus Oliveira Date: Wed, 12 Dec 2018 18:44:59 +0000 Subject: [PATCH 4/4] removed trailing spaces --- src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js b/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js index bc6a4e32..1f0da747 100644 --- a/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js +++ b/src/algorithms/math/fibonacci/fibonacciRecursiveMemoized.js @@ -1,5 +1,5 @@ /* memoize function use a Map to store function arguments - and results, which are returned if found inside cache */ +and results, which are returned if found inside cache */ const memoize = (fn) => { const cache = new Map(); @@ -13,7 +13,7 @@ const memoize = (fn) => { }; /* fibonacci function decorated with the memoize function, every call - will first pass through the cache before it's computed */ +will first pass through the cache before it's computed */ const fibonacci = memoize((n) => { if (n === 0 || n === 1) return n;