From c16b4fe4c3bfc732d892be7834bcea530c14f58f Mon Sep 17 00:00:00 2001 From: ukyon Date: Sat, 23 Jan 2021 22:48:09 +0900 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 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app.js b/app.js index ad9a93a7..e41beb1f 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 3aaca737bb300615e86d61617d1d3a5799ae2e9d Mon Sep 17 00:00:00 2001 From: ukyon Date: Sat, 23 Jan 2021 23:02:59 +0900 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 | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index e41beb1f..f0973cba 100644 --- a/app.js +++ b/app.js @@ -1,11 +1,14 @@ '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); } - return fib(n-1)+fib(n-2); + const value=fib(n-1)+fib(n-2); + memo.set(n,value); + return value; } const length = 40;