-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatkin.py
37 lines (36 loc) · 1.21 KB
/
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
import math
def atkin(nmax):
"""
Returns a list of prime numbers below the number "nmax"
"""
is_prime = dict([(i, False) for i in range(5, nmax+1)])
for x in range(1, int(math.sqrt(nmax))+1):
for y in range(1, int(math.sqrt(nmax))+1):
n = 4*x**2 + y**2
if (n <= nmax) and ((n % 12 == 1) or (n % 12 == 5)):
is_prime[n] = not is_prime[n]
n = 3*x**2 + y**2
if (n <= nmax) and (n % 12 == 7):
is_prime[n] = not is_prime[n]
n = 3*x**2 - y**2
if (x > y) and (n <= nmax) and (n % 12 == 11):
is_prime[n] = not is_prime[n]
for n in range(5, int(math.sqrt(nmax))+1):
if is_prime[n]:
ik = 1
while (ik * n**2 <= nmax):
is_prime[ik * n**2] = False
ik += 1
primes = []
for i in range(nmax + 1):
if i in [0, 1, 4]: pass
elif i in [2,3] or is_prime[i]: primes.append(i)
else: pass
return primes
# Test
# assert(atkin(number)==[2, 3, 5, 7, 11, 13, 17, 19, 23, 29])
# User Input
number_input = input("Enter maximum value to compute primes: ")
#Type Cast Input
number = int(number_input)
print(atkin(number))