From 868ffd13cb928f95019c0292de05fa8caa9fd8e1 Mon Sep 17 00:00:00 2001 From: reicch Date: Sat, 27 Mar 2021 13:46:49 +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 | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app.js b/app.js index ad9a93a7..fc5d75db 100644 --- a/app.js +++ b/app.js @@ -1 +1,12 @@ 'use strict'; +function fib(n){ +if (n===0){ + return 0; + }else if (n===1){ + return 1; + } + return fib(n-1) + fib(n-2); +} +for (let i = 0; i <= 40; i++){ + console.log( i + "のフィボナッチ数は" + fib(i) + "です。"); +} \ No newline at end of file From a13f9108dcf501ffde660050154b9bee7a3fdcb2 Mon Sep 17 00:00:00 2001 From: reicch Date: Sat, 27 Mar 2021 14:36:04 +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, 10 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index fc5d75db..82cc2c06 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,16 @@ 'use strict'; +const memo = new Map(); +//n=0,n=1は最初から決まってる +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); + let value = fib(n-1) + fib(n-2); + memo.set(n, value); + return value; } -for (let i = 0; i <= 40; i++){ +for (let i = 0; i <= 100; i++){ console.log( i + "のフィボナッチ数は" + fib(i) + "です。"); } \ No newline at end of file From 2a3fb2f793583ad39f55ca61d76c11079cdfa604 Mon Sep 17 00:00:00 2001 From: reicch Date: Sun, 28 Mar 2021 14:07:59 +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=E3=81=AE=E3=82=A2=E3=83=AA=E3=82=B4=E3=83=AA?= =?UTF-8?q?=E3=82=BA=E3=83=A0=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 82cc2c06..28816620 100644 --- a/app.js +++ b/app.js @@ -2,15 +2,17 @@ const memo = new Map(); //n=0,n=1は最初から決まってる 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); } - let value = fib(n-1) + fib(n-2); + let value = fib(n-1) + fib(n-2) + fib(n-3); memo.set(n, value); return value; } -for (let i = 0; i <= 100; i++){ - console.log( i + "のフィボナッチ数は" + fib(i) + "です。"); +const length = 40; +for (let i = 0; i <= length; i++){ + console.log( i + "のトリボナッチ数は" + fib(i) + "です。"); } \ No newline at end of file