From 61bdb902a7b37d96acce122961e52d4002707eba Mon Sep 17 00:00:00 2001 From: shota morita Date: Sun, 31 Jan 2021 10:51:18 +0900 Subject: [PATCH 1/4] =?UTF-8?q?O=E8=A8=98=E6=B3=95=E3=81=AEfib=E9=96=A2?= =?UTF-8?q?=E6=95=B0=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app.js b/app.js index ad9a93a7..a4b5dbfc 100644 --- a/app.js +++ b/app.js @@ -1 +1,15 @@ 'use strict'; +console.time('fib'); +function fib(n){ + if(n === 0){ + return 0; + }else if(n === 1){ + return 1; + } + return fib(n - 1)+ fib(n - 2); +} +let l = process.argv[2] || 10; +for(let i = 0; i <= l; i++){ + console.log(fib(i)); +} +console.timeEnd('fib'); \ No newline at end of file From 5e70a3cde8079945a6b2c1fdc7085015df520071 Mon Sep 17 00:00:00 2001 From: shota morita Date: Sun, 31 Jan 2021 11:05:07 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E9=85=8D=E5=88=97=E3=81=AB=E3=82=88?= =?UTF-8?q?=E3=82=8B=E3=82=A2=E3=83=AB=E3=82=B4=E3=83=AA=E3=82=BA=E3=83=A0?= =?UTF-8?q?=E3=81=AE=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index a4b5dbfc..0033a8cf 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,15 @@ 'use strict'; console.time('fib'); +const memo = new Map(); +memo.set(0, 0); +memo.set(1, 1); function fib(n){ - if(n === 0){ - return 0; - }else if(n === 1){ - return 1; + if(memo.has(n)){ + return memo.get(n); } - return fib(n - 1)+ fib(n - 2); + const v = fib(n - 1) + fib(n - 2); + memo.set(n, v); + return v; } let l = process.argv[2] || 10; for(let i = 0; i <= l; i++){ From ee1d42fb63cb39ec370f86d09976ca9a7058d826 Mon Sep 17 00:00:00 2001 From: shota morita Date: Sun, 31 Jan 2021 11:24:49 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E3=83=88=E3=83=AA=E3=83=9C=E3=83=8A?= =?UTF-8?q?=E3=83=83=E3=83=81=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 app.js diff --git a/app.js b/app.js deleted file mode 100644 index 0033a8cf..00000000 --- a/app.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; -console.time('fib'); -const memo = new Map(); -memo.set(0, 0); -memo.set(1, 1); -function fib(n){ - if(memo.has(n)){ - return memo.get(n); - } - const v = fib(n - 1) + fib(n - 2); - memo.set(n, v); - return v; -} -let l = process.argv[2] || 10; -for(let i = 0; i <= l; i++){ - console.log(fib(i)); -} -console.timeEnd('fib'); \ No newline at end of file From be9bb2998ae5499f1a5885ebd5476e53a3fbff84 Mon Sep 17 00:00:00 2001 From: shota morita Date: Sun, 31 Jan 2021 11:26:25 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E3=83=AA=E3=83=88=E3=83=A9=E3=82=A4:?= =?UTF-8?q?=E3=83=88=E3=83=AA=E3=83=9C=E3=83=8A=E3=83=83=E3=83=81=E3=81=AE?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fib.js | 19 +++++++++++++++++++ trib.js | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 fib.js create mode 100644 trib.js diff --git a/fib.js b/fib.js new file mode 100644 index 00000000..28707bff --- /dev/null +++ b/fib.js @@ -0,0 +1,19 @@ +'use strict'; +console.time('fib'); +const memo = new Map(); +memo.set(0, 0); +memo.set(1, 0); +memo.set(2, 1); +function fib(n){ + if(memo.has(n)){ + return memo.get(n); + } + const v = fib(n - 1) + fib(n - 2); + memo.set(n, v); + return v; +} +let l = process.argv[2] || 10; +for(let i = 0; i <= l; i++){ + console.log(fib(i)); +} +console.timeEnd('fib'); \ No newline at end of file diff --git a/trib.js b/trib.js new file mode 100644 index 00000000..67cf240f --- /dev/null +++ b/trib.js @@ -0,0 +1,19 @@ +'use strict'; +console.time('trib'); +const memo = new Map(); +memo.set(0, 0); +memo.set(1, 0); +memo.set(2, 1); +function trib(n){ + if(memo.has(n)){ + return memo.get(n); + } + const v = trib(n - 1) + trib(n - 2) + trib(n - 3); + memo.set(n, v); + return v; +} +let l = process.argv[2] || 10; +for(let i = 0; i <= l; i++){ + console.log(trib(i)); +} +console.timeEnd('trib'); \ No newline at end of file