题解 | #质数因子#
质数因子
https://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
// 质数因子
const judgeNum=(a)=>{//判断是否是质数
if(a==1||a==2)
{
return true
}
else{
let i=2
while(i<=Number(Math.sqrt(a)))
{
if(a%i==0)
{
return false
}
else{
i++
}
}
return true
}
}
const getItemFn=(num)=>{//求质数因子
let arr =[]
let other = num,i=2// other除余 i 可能因子
while(!judgeNum(other)){//判断质数-循环结束
if(judgeNum(i))//是质数才开始判断加入
{
if(other%i==0)//判断质因子
{
other=other/i
arr.push(i)
}
else{
i++
}
}
else{
i++
}
}
arr.push(other)
return arr.sort((a,b)=>a-b).join(' ')
}
void async function () {
// Write your code here
let line = await readline()
console.log(getItemFn(line))
}()
查看23道真题和解析