fork download
  1. const memo = new Map();
  2.  
  3. function hitungPohonFaktor(n) {
  4. if (memo.has(n)) {
  5. return memo.get(n);
  6. }
  7.  
  8. let maxSum = n;
  9.  
  10. for (let i = 2; i <= Math.sqrt(n); i++) {
  11. if (n % i === 0) {
  12. const currentSum = n + hitungPohonFaktor(i) + hitungPohonFaktor(n / i);
  13.  
  14. if (currentSum > maxSum) {
  15. maxSum = currentSum;
  16. }
  17. }
  18. }
  19.  
  20. memo.set(n, maxSum);
  21. return maxSum;
  22. }
  23.  
  24. console.log(hitungPohonFaktor(6));
  25. console.log(hitungPohonFaktor(12));
  26. console.log(hitungPohonFaktor(16));
  27. console.log(hitungPohonFaktor(18));
  28. console.log(hitungPohonFaktor(24));
  29. console.log(hitungPohonFaktor(36));
  30. console.log(hitungPohonFaktor(48));
  31. console.log(hitungPohonFaktor(64));
Success #stdin #stdout 0.05s 40684KB
stdin
Standard input is empty
stdout
11
25
36
35
51
73
101
136