/*给任意一个偶数,找出他的所有的质数因子*/
function primeFactor(n){
var factors=[],
divistor=2;
if(typeof n !=='number'||!Number.isInteger(n)){
return 0;
if(n%divistor===0){
factors.push(divistor);
n=n/divistor;
}else{
divistor++;
}
}
return factors;
}
alert(primeFactor(3));
function primeFactor(n){
var factors=[],
divistor=2;
if(typeof n !=='number'||!Number.isInteger(n)){
return 0;
};
//如果不是偶数返回0,如果是0,返回0
if(n%2!==0||n===0){return 0;}
//返回所有质数因子
while(n>=2){if(n%divistor===0){
factors.push(divistor);
n=n/divistor;
}else{
divistor++;
}
}
return factors;
}
alert(primeFactor(3));