sum(num) { var p = new Array p[0] = 1 var j = 0 var k = 0 var s = 0 if (num<=0){return 0} for (var i = 1; i<num+1;i++) { j=1 k=1 s=0 while (j>0){ j = i-(3*k*k+k)/2 if (j>=0) {s -= Math.pow(-1,k)*p[j]} j = i-(3*k*k-k)/2 if (j>=0) {s -= Math.pow(-1,k)*p[j]} k = k+1 } p[i] = s } return p[num] }
number? var memo = []; function sum(n, m = n) { if (n == 0) return 1; if (n < 0 || m == 0) return 0; if (memo[n] && memo[n][m]) return memo[n][m]; let total = sum(n, m - 1) + sum(n - m, m); if (!memo[n]) { memo[n] = []; } memo[n][m] = total; return total; }