Skip to content

Instantly share code, notes, and snippets.

@gyaresu
Created May 11, 2012 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyaresu/2657161 to your computer and use it in GitHub Desktop.
Save gyaresu/2657161 to your computer and use it in GitHub Desktop.
Sum of primes under 2e6
var list = [];
var primes = [];
var start = 2e6;
var total = 0;
function range(x) {
for (i=2; i<=x; i++) {
list.push(i);
}
}
range(start);
function remove(x) {
if (x % list[0] === 0) {
return false;
} else {
return true;
}
}
while (list.length !== 0) {
//console.log(list);
primes.push(list[0]);
list = list.filter(remove);
}
for (i=0; i<primes.length; i++) {
total += primes[i];
}
console.log("The sum of all primes is: " + total);
//console.log(primes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment