added js version of problem 10

This commit is contained in:
ikselven 2017-07-04 00:13:29 +02:00
parent be5d7c1444
commit d8d17c0dbd

36
10/euler010.js Executable file
View file

@ -0,0 +1,36 @@
#!/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);