From 9518972737a355e0a9350814861430cf6ddbf9a7 Mon Sep 17 00:00:00 2001 From: hioryuki takata Date: Sat, 18 May 2024 23:36:09 +0000 Subject: [PATCH 1/2] =?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 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app.js b/app.js index ad9a93a7..d35d3a89 100644 --- a/app.js +++ b/app.js @@ -1 +1,19 @@ 'use strict'; + +function fib(n){ + if (n === 0){ + return 0; + }else if (n === 1){ + return 1;} + else{ + return fib(n-1) +fib(n -2); + } +}; + +const length = 100; +for (let i=0; i<=length; i++){ + console.log(fib(i)); +}; + + + From 72fc29fc28f39d30153f84f0aae2753c756fc7a2 Mon Sep 17 00:00:00 2001 From: hioryuki takata Date: Sat, 18 May 2024 23:43:11 +0000 Subject: [PATCH 2/2] =?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 | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index d35d3a89..caa45b9a 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,16 @@ '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;} + if (memo.has(n)){ + return memo.get(n); + } else{ - return fib(n-1) +fib(n -2); + const value = fib(n-1)+fib(n-2); + memo.set(n, value); + return value; } };