Created
July 22, 2011 11:16
-
-
Save rik0/1099261 to your computer and use it in GitHub Desktop.
Python Atkin
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
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