编程语言
首页 > 编程语言> > Javascript 求质数因数

Javascript 求质数因数

作者:互联网

功能:输入一个正整数,按照从小到大的顺序输出它的所有质因子(重复的也要列举)(如180的质因子为2 2 3 3 5 )

 

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
rl.on('line', function (line) {
  let num = parseInt(line);
  function getCode(num){
    const sqrt = Math.sqrt(num);
    const arr = [];
    for (let i = 2; i <= sqrt; i++){
      while(num % i == 0){
        arr.push(i);
        num /= i;
      }
    }
    if(num != 1){
      arr.push(num);
    }
    return arr.join(" ");
  }

  let res = getCode(num);
  console.log(res)
});

标签:arr,const,质数,Javascript,因数,num,let,line,readline
来源: https://www.cnblogs.com/ran2022/p/15824880.html