-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimenumbers.py
More file actions
46 lines (34 loc) · 894 Bytes
/
primenumbers.py
File metadata and controls
46 lines (34 loc) · 894 Bytes
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
import decorators
# Sieve of Eratosthenes
@decorators.timeit
def primes_sieve(limit):
""" Return list of primes smaller than limit with indexes marked as true. O(n(logn)(loglogn)) """
a = [True] * limit
a[0] = a[1] = False
for(i, prime) in enumerate(a):
if prime:
# yield i # Turn into generator
for n in range(i*i, limit, i):
a[n] = False
return a
@decorators.timeit
def isprime(n):
""" Returns True if n is prime using deterministic AKS primality test. O(log(n)^12) """
if n == 2:
return True
if n == 3:
return True
if n % 2 == 0:
return False
if n % 3 == 0:
return False
i = 5
w = 2
while i * i <= n:
if n % i == 0:
return False
i += w
w = 6 - w
return True
primes_sieve(2000000)
isprime(508012429)