[Algorithm] Finding all factors of a number

12's factors are: {1,2,3,4,6,12}

function factors (n) {
   let list = [];
  
  for (let i = 1; i < Math.sqrt(n); i++) {
    if (n % i === 0) {
      list.push(i);
      if (i !== Math.sqrt(n)) {
          list.push(n / i);
      }
    }
  }
  
  return list;
}

factors(12) // [ 1, 12, 2, 6, 3, 4 ]
原文地址:https://www.cnblogs.com/Answer1215/p/10865376.html