algorithms/eratosthenes.js
2024-01-12 07:01:06 -05:00

15 lines
467 B
JavaScript

function sumPrimes(num) {
const isPrime = Array(num + 1).fill(true);
isPrime[0] = false;
isPrime[1] = false;
for (let prime = 2; prime < Math.sqrt(num); prime++) {
if (isPrime[prime]) {
for (let multiple = prime * prime; multiple <= num; multiple += prime) {
isPrime[multiple] = false;
}
}
}
return isPrime.reduce((sum, prime, index) => (prime ? sum + index : sum), 0);
// return isPrime;
}
console.log(sumPrimes(10000000));