题解 | #质数因子#

质数因子

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)) 
    
}()

全部评论

相关推荐

头像
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务