Skip to content

Instantly share code, notes, and snippets.

@wowdyaln
Created May 26, 2017 07:26
Show Gist options
  • Save wowdyaln/7529605a3138f54bd78d4ea60522bd81 to your computer and use it in GitHub Desktop.
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.
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