[Algorithm] Finding all factors of a number
Posted answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 ]
以上是关于[Algorithm] Finding all factors of a number的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 750 F:New Year and Finding Roots
PAT甲题题解-1108. Finding Average (20)-字符串处理