Created
May 10, 2016 10:53
-
-
Save alihammad-gist/e032cd112d0d1eeb40badb6c1432a68d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function sumPrimes(num) { | |
var p = []; | |
p[num] = undefined; // initializing array till 0 -> num | |
p[0] = false; // not prime | |
p[1] = false; // not prime | |
for (var i = 2; i * i <= num; i+=1) { | |
if (p[i] === undefined) { // if its a prime | |
// cross off all the composites divisible by i | |
for (var j = 2; i * j <= num; j+=1) { | |
p[i*j] = false; | |
} | |
} | |
} | |
var sum = 0; | |
for (var k = 2; k <= num; k++) { | |
if (p[k] === undefined) { | |
sum += k; | |
} | |
} | |
return sum; | |
} | |
sumPrimes(10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment