Answers for "prime factors of an integer js"

0

prime factors of an integer js

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
Posted by: Guest on October-16-2021

Code answers related to "prime factors of an integer js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language