added js version of problem 10
This commit is contained in:
parent
be5d7c1444
commit
d8d17c0dbd
1 changed files with 36 additions and 0 deletions
36
10/euler010.js
Executable file
36
10/euler010.js
Executable 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);
|
Reference in a new issue