Answers for "How could you find all prime factors of a number?"

1

How could you find all prime factors of a number?

function primeFactors(n){
  var factors = [], 
      divisor = 2;
  
  while(n>2){
    if(n % divisor == 0){
       factors.push(divisor); 
       n= n/ divisor;
    }
    else{
      divisor++;
    }     
  }
  return factors;
}

> primeFactors(69);
  = [3, 23]
Posted by: Guest on May-21-2021
0

Prime factors of a number

public List<Integer> factorsOf(int n) {
  ArrayList<Integer> factors = new ArrayList<>();

  for (int d = 2; n > 1; d++)
    for (; n % d == 0; n /= d)
      factors.add(d);

  return factors;
}
Posted by: Guest on May-13-2021

Code answers related to "How could you find all prime factors of a number?"

Code answers related to "Javascript"

Browse Popular Code Answers by Language