@seungha/

fiboRecMemoized

ES6

No description

fork
loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.9313732795276162","path":"main.js","file":{"path":"main.js","content":{"asEncoding":{"base64":"ZnVuY3Rpb24gZmlib1JlYyhuKSB7CiAgcmV0dXJuICgKICAgIG4gPCAxID8gMCA6CiAgICBuID09PSAxID8gMSA6CiAgICBmaWJvUmVjKG4gLSAxKSArIGZpYm9SZWMobiAtIDIpCiAgKTsKfQoKY29uc3QgZmlib1JlY01lbW9pemVkID0gKCgpID0+IHsKICBjb25zdCBtZW1vID0gbmV3IE1hcCgpOwogIGNvbnN0IGZpYm9SZWMgPSBuID0+IHsKICAgIGxldCByZXN1bHQgPSBtZW1vLmdldChuKTsKICAgIGlmIChyZXN1bHQgIT0gdW5kZWZpbmVkKSByZXR1cm4gcmVzdWx0OwogICAgcmVzdWx0ID0gKAogICAgICBuIDwgMSA/IDAgOgogICAgICBuID09PSAxID8gMSA6CiAgICAgIGZpYm9SZWMobiAtIDEpICsgZmlib1JlYyhuIC0gMikKICAgICk7CiAgICBtZW1vLnNldChuLCByZXN1bHQpOwogICAgcmV0dXJuIHJlc3VsdDsKICB9CiAgcmV0dXJuIGZpYm9SZWM7Cn0pKCk7Cgpjb25zb2xlLnRpbWUoJ2ZpYm9SZWMnKTsKZmlib1JlYyg0MCk7CmNvbnNvbGUudGltZUVuZCgnZmlib1JlYycpOwoKY29uc29sZS50aW1lKCdmaWJvUmVjTWVtb2l6ZWQnKTsKZmlib1JlY01lbW9pemVkKDQwKTsKY29uc29sZS50aW1lRW5kKCdmaWJvUmVjTWVtb2l6ZWQnKTsK"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function fiboRec(n) {
  return (
    n < 1 ? 0 :
    n === 1 ? 1 :
    fiboRec(n - 1) + fiboRec(n - 2)
  );
}

const fiboRecMemoized = (() => {
  const memo = new Map();
  const fiboRec = n => {
    let result = memo.get(n);
    if (result != undefined) return result;
    result = (
      n < 1 ? 0 :
      n === 1 ? 1 :
      fiboRec(n - 1) + fiboRec(n - 2)
    );
    memo.set(n, result);
    return result;
  }
  return fiboRec;
})();

console.time('fiboRec');
fiboRec(40);
console.timeEnd('fiboRec');

console.time('fiboRecMemoized');
fiboRecMemoized(40);
console.timeEnd('fiboRecMemoized');
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie