📜  整数 js 的素数 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:02:41.582000             🧑  作者: Mango

代码示例1
function primeFactors(n) {
  const factors = [];
  let divisor = 2;

  while (n >= 2) {
    if (n % divisor == 0) {
      factors.push(divisor);
      n = n / divisor;
    } else {
      divisor++;
    }
  }
  return factors;
}

const randomNumber = Math.floor(Math.random() * 10000);
console.log('Prime factors of', randomNumber + ':', primeFactors(randomNumber).join(' '))
 Run code snippet