题解 | #质数因子#
质数因子
http://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
while(line = readline()) {
var num = parseInt(line)
// 180 2 2 3 3 5
var result = []
function calc(n) {
if (isPrime(n)) {
result.push(n)
return
}
for (var i = 2; i <= n; i++) {
if (isPrime(i) && n % i === 0) {
n = n / i
result.push(i)
calc(n)
break;
}
}
}
calc(num)
console.log(result.join(' ') + ' ')
}
function isPrime(num) {
if (num <=3) {
return num > 1;
}
if (num % 6 !== 1 && num % 6 !== 5) {
return false;
}
var sqrt = Math.sqrt(num);
for (var i = 5; i <= sqrt; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) {
return false;
}
}
return true;
}