From 3c600d0f5a71b056eacf1fb3c6343f2a1e7e6892 Mon Sep 17 00:00:00 2001 From: d_nakayama Date: Wed, 18 Nov 2020 14:05:24 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E6=8C=87=E6=95=B0=E3=82=AA=E3=83=BC?= =?UTF-8?q?=E3=83=80=E3=83=BC=E3=81=AEfib=E9=96=A2=E6=95=B0=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 --- app.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app.js b/app.js index ad9a93a7..551254b8 100644 --- a/app.js +++ b/app.js @@ -1 +1,14 @@ 'use strict'; +function fib(n) { + if (n === 0) { + return 0; + } else if (n === 1) { + return 1; + } + return fib(n - 1) + fib(n - 2); + } + +const length = 40; +for (let i = 0; i <= length; i++) { + console.log(fib(i)); +} \ No newline at end of file From 51ce4314112a16f92b92aa26ce177a38b7ba3a2c Mon Sep 17 00:00:00 2001 From: d_nakayama Date: Wed, 18 Nov 2020 14:12:57 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E3=83=A1=E3=83=A2=E5=8C=96=E3=81=AB?= =?UTF-8?q?=E3=82=88=E3=82=8B=E3=82=A2=E3=83=AB=E3=82=B4=E3=83=AA=E3=82=BA?= =?UTF-8?q?=E3=83=A0=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 | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index 551254b8..78732efc 100644 --- a/app.js +++ b/app.js @@ -1,13 +1,15 @@ 'use strict'; +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; - } - return fib(n - 1) + fib(n - 2); + if (memo.has(n)) { + return memo.get(n); } - + const value = fib(n - 1) + fib(n - 2); + memo.set(n, value); + return value; +} const length = 40; for (let i = 0; i <= length; i++) { console.log(fib(i)); From 953b122cbb1b5b10317fc5f30b50fdb52ac08507 Mon Sep 17 00:00:00 2001 From: d_nakayama Date: Wed, 18 Nov 2020 14:17:14 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E3=83=88=E3=83=AA=E3=83=9C=E3=83=8A?= =?UTF-8?q?=E3=83=83=E3=83=81=E6=95=B0=E5=88=97=E3=81=B8=E3=81=AE=E6=94=B9?= =?UTF-8?q?=E4=BF=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 78732efc..b42ef5cb 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,14 @@ 'use strict'; const memo = new Map(); memo.set(0, 0); -memo.set(1, 1); +memo.set(1, 0); +memo.set(2, 1); + function fib(n) { if (memo.has(n)) { return memo.get(n); } - const value = fib(n - 1) + fib(n - 2); + const value = fib(n - 1) + fib(n - 2) + fib(n - 3); memo.set(n, value); return value; }