码迷,mamicode.com
首页 > 其他好文 > 详细

[Algorithm] Finding all factors of a number

时间:2019-05-15 00:47:40      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:lis   tor   ber   cti   function   bsp   nbsp   fun   mat   

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 ]

 

[Algorithm] Finding all factors of a number

标签:lis   tor   ber   cti   function   bsp   nbsp   fun   mat   

原文地址:https://www.cnblogs.com/Answer1215/p/10865376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!