This repository has been archived on 2024-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
project-euler/e0010/euler0010.js

37 lines
737 B
JavaScript
Raw Normal View History

2017-07-04 00:13:29 +02:00
#!/usr/bin/node
function getPrimeList(max) {
if (typeof max !== 'number' || max < 2) {
return [];
} else if (max===2) {
return [2];
}
let primes = [2];
for(i=3; i<=max; i++) {
let isPrime = true;
for(prime of primes) {
if (i % prime === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(i);
}
}
return primes;
}
console.time();
let primeList = getPrimeList(2000001);
console.timeEnd();
console.log("Found "+primeList.length+" prime numbers.");
console.time();
let primeSum = primeList.reduce( (sum, element) => sum + element);
console.timeEnd();
console.log(primeSum);