Created
May 26, 2017 07:26
-
-
Save wowdyaln/7529605a3138f54bd78d4ea60522bd81 to your computer and use it in GitHub Desktop.
slightly modified the Sieve of Sundaram algorithm to cut the unnecessary iterations and it seems to be very fast.
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
function primeSieve(n){ | |
var a = Array(n = n/2), | |
t = (Math.sqrt(4+8*n)-2)/4, | |
u = 0, | |
r = []; | |
for(var i = 1; i <= t; i++){ | |
u = (n-i)/(1+2*i); | |
for(var j = i; j <= u; j++) a[i + j + 2*i*j] = true; | |
} | |
for(var i = 0; i<= n; i++) !a[i] && r.push(i*2+1); | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment