Skip to content

Instantly share code, notes, and snippets.

@rik0
Created July 22, 2011 11:16
Show Gist options
  • Save rik0/1099261 to your computer and use it in GitHub Desktop.
Save rik0/1099261 to your computer and use it in GitHub Desktop.
Python Atkin
def atkin(buffer, end):
limit = int(math.ceil(math.sqrt(end)))
for x in range(1, limit + 1):
x2 = x * x
x2_3 = 3 * x2
for y in range(1, limit + 1):
y2 = y * y
n = 4 * x2 + y2
if (n < end) and (n % 12 == 1 or n % 12 == 5):
buffer[n] = not buffer[n]
n = x2_3 + y2
if (n < end) and (n % 12 == 7):
buffer[n] = not buffer[n]
n = x2_3 - y2
if (x > y) and (n < end) and (n % 12 == 11):
buffer[n] = not buffer[n]
for n in range(5, limit+1):
if buffer[n]:
k = n * n
m = k
i = 1
while m < end:
buffer[m] = False
i += 2
m = i * k
buffer[2] = True
buffer[3] = True
return buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment