From 3c69861ab71fc2b9ea00cafa45abf6f64cdc8305 Mon Sep 17 00:00:00 2001 From: Grzegorz Kucmierz Date: Tue, 19 Nov 2019 10:29:52 +0100 Subject: [PATCH] Perimeter of squares in a rectangle --- perimeter-of-squares-in-a-rectangle/index.js | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 perimeter-of-squares-in-a-rectangle/index.js diff --git a/perimeter-of-squares-in-a-rectangle/index.js b/perimeter-of-squares-in-a-rectangle/index.js new file mode 100644 index 0000000..3f13ffc --- /dev/null +++ b/perimeter-of-squares-in-a-rectangle/index.js @@ -0,0 +1,23 @@ +// https://www.codewars.com/kata/perimeter-of-squares-in-a-rectangle/javascript + +const perimeter = (() => { + const map = new WeakMap(); + const f = n => { + if (n in map) return map[n]; + if (n === 0) return fib(n) * 4; + return map[n] = 4 * fib(n) + f(n-1); + }; + return f; +})(); + +// https://gist.github.com/gkucmierz/9bc8126bdb8d42a17ccb25f81bf02519 +const fib = ((fst, snd) => { + const map = new WeakMap(); + const f = n => { + if (n in map) return map[n]; + if (n === 0) return fst; + if (n === 1) return snd; + return map[n] = f(n-1) + f(n-2); + }; + return f; +})(1, 1);