encounters a javascript algorithm problem, which requires that find out all prime factors of a number
algorithm I designed:
function primeFactors(n){
var factors = [];
var divisor = 3;
if (n % 2==0) factors.push(2);
while(n>5){
if(n % divisor == 0){
!factors.includes(divisor) && factors.push(divisor);
n= n/ divisor;
}
else{
divisor+=2;
}
}
return factors;
};
console.log(primeFactors(666));
the running result is
although the operation results seem to be correct, I feel that the design of the algorithm is too low and the performance is low. I would like to ask for the help of God to analyze and write a better algorithm. Thank you.