-
Notifications
You must be signed in to change notification settings - Fork 2
/
sieve_of_atkin.py
executable file
·60 lines (44 loc) · 1.31 KB
/
sieve_of_atkin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from math import sqrt, ceil, pow
class SieveOfAtkin:
def __init__(self, limit):
self.limit = limit
self.primes = []
self.sieve = [False]*(self.limit+1)
def flip(self, prime):
try:
self.sieve[prime] = True if self.sieve[prime] == False else False
except KeyError:
pass
def invalidate(self, prime):
try:
if self.sieve[prime] == True: self.sieve[prime] = False
except KeyError:
pass
def isPrime(self, prime):
try:
return self.sieve[prime]
except KeyError:
return False
def getPrimes(self):
testingLimit = int(ceil(sqrt(self.limit)))
for i in range(testingLimit):
for j in range(testingLimit):
# n = 4*i^2 + j^2
n = 4*int(pow(i, 2)) + int(pow(j,2))
if n <= self.limit and (n % 12 == 1 or n % 12 == 5):
self.flip(n)
# n = 3*i^2 + j^2
n = 3*int(pow(i, 2)) + int(pow(j,2))
if n <= self.limit and n % 12 == 7:
self.flip(n)
# n = 3*i^2 - j^2
n = 3*int(pow(i, 2)) - int(pow(j,2))
if n <= self.limit and i > j and n % 12 == 11:
self.flip(n)
for i in range(5, testingLimit):
if self.isPrime(i):
k = int(pow(i, 2))
for j in range(k, self.limit, k):
self.invalidate(j)
self.primes = [2, 3] + [x for x in range(len(self.sieve)) if self.isPrime(x) and x>=5]
return self.primes