Ponchimeow/Practice

友好數

Opened this issue · 0 comments

Lidemy OJ, 友好數


function pe(input) {
  const n = parseInt(input);
  let nSum = factor(n).reduce(function (acc, cur) {
    return acc + cur
  });
  if (nSum === 1) return `0`;
  if (nSum === n) return `=${n}`;
  let mSum = factor(nSum).reduce(function (acc, cur) {
    return acc + cur
  });
  if (mSum === n) return `${nSum}`;
  return `0`;
}

function factor(num) {
  let factor = [];
  for (let i = 1; i < num; i += 1) {
    if (num % i === 0) factor.push(i);
  }
  return factor;
}