From 2830ddbaaf755a810a11ce24a839248fd93f5942 Mon Sep 17 00:00:00 2001 From: heso Date: Sat, 21 Aug 2021 02:57:09 +0000 Subject: [PATCH] =?UTF-8?q?=E3=83=88=E3=83=AA=E3=83=9C=E3=83=8A=E3=83=83?= =?UTF-8?q?=E3=83=81=E6=95=B0=E5=88=97=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 | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index bba68419..1629eca7 100644 --- a/app.js +++ b/app.js @@ -1,16 +1,17 @@ 'use strict'; + const memo = new Map(); memo.set(0, 0); -memo.set(1, 1); -function trib(n) { - if (memo.has(n)) { - return memo.get(n); - } - const value = trib(n - 1) + trib(n - 2); +memo.set(1, 0); +memo.set(2, 1); + +function trib(n) +{ + if (memo.has(n)) return memo.get(n); + const value = trib(n-1) + trib(n-2) + trib(n-3); memo.set(n, value); return value; } + const length = 40; -for (let i = 0; i <= length; i++) { - console.log(trib(i)); -} +for (let i = 0; i <= length; i++) console.log(trib(i));