Created
May 11, 2012 02:41
-
-
Save gyaresu/2657161 to your computer and use it in GitHub Desktop.
Sum of primes under 2e6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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