Skip to content

Instantly share code, notes, and snippets.

@19h
Created May 29, 2013 14:03
Show Gist options
  • Select an option

  • Save 19h/5670486 to your computer and use it in GitHub Desktop.

Select an option

Save 19h/5670486 to your computer and use it in GitHub Desktop.
Javascript Prime-Generator (Atkins-Sieve)
atkinsSieve = function (e) {
last = 0;
for (var d = Array(e + 1), a = 0; a < d.length; a++) d[a] = 0;
for (var f = Math.floor(Math.sqrt(e)) + 1, b, c, a = 1; a < f; a++) for (c = 1; c < f; c++) {
b = 4 * Math.pow(a, 2) + Math.pow(c, 2);
if (b <= e && (1 === b % 12 || 5 === b % 12)) d[b] ^= 1;
b = 3 * Math.pow(a, 2) + Math.pow(c, 2);
b <= e && 7 === b % 12 && (d[b] ^= 1);
a > c && (b = 3 * Math.pow(a, 2) - Math.pow(c, 2), b <= e && 11 === b % 12 && (d[b] ^= 1))
}
for (a = 5; a < f; a++) if (1 === d[a]) for (c = Math.pow(a, 2); c < e; c += Math.pow(a, 2)) d[c] = 0;
for (a = 7; a < e; a++) 1 === d[a] && (last = a);
return last
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment