Sum All Primes

时间:2021-09-14 21:41:14
function sumPrimes(num) {
//return num;
var arr = [];
var ifPrime = function(num){
if(num < 2){
return false;
}
if(num === 2){
return true;
}
if(num % 2 === 0){
return false;
}
for(var i = 3; i <= Math.sqrt(num); i+=2){
if(num % i === 0){
return false;
}
}
return true;
};
for(var i = 2; i <= num; i++){
if(ifPrime(i)){
arr.push(i);
}
}
return arr.reduce(function(prev,cur,index,array){
return prev + cur;
},0);
} sumPrimes(10);