-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes.py
More file actions
75 lines (67 loc) · 1.89 KB
/
primes.py
File metadata and controls
75 lines (67 loc) · 1.89 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
"""
Module containing method for finding primes
"""
import numpy as np
def primes_bruteforce(limit):
"""
Prime number generator (upto a given limit)
with a sort-of bruteforce method
"""
primes = [2]
for i in range(3, limit+1, 2):
j = 0
is_prime = True
while primes[j]*primes[j] <= i:
if i % primes[j] == 0:
is_prime = False
break
j += 1
if is_prime:
primes.append(i)
return primes
def eratosthenes(limit):
"""
Calculate all primes up to limit using the sieve of Eratosthenes method
"""
if isinstance(limit, (int, float)) and limit == int(limit):
limit = int(limit)
else:
raise ValueError
primes = []
mask = [1]*(limit+1)
for i in range(2, limit+1):
if mask[i]:
primes.append(i)
for j in range(i*i, limit+1, i):
mask[j] = 0
return primes
def eratosthenes_np(limit):
"""
Calculate all primes up to limit using the sieve of Eratosthenes method
utilizing numpy arrays and methods
"""
if isinstance(limit, (int, float)):
limit = int(limit)
else:
raise ValueError
mask = np.ones(limit+1, dtype=np.bool)
mask[:2] = False
for i in range(2, int(np.sqrt(limit))+1):
if mask[i]:
mask[i*i::i] = False
return np.nonzero(mask)[0]
def eratosthenes_npo(limit):
"""
Calculate all primes up to limit using the sieve of Eratosthenes method
utilizing numpy arrays and methods, trying to conserve memory
"""
if isinstance(limit, (int, float)):
limit = int(limit)
else:
raise ValueError
mask = np.ones(limit//2, dtype=np.bool)
for i in range(3, int(limit**0.5)+1, 2):
if mask[i//2]:
mask[i*i//2::i] = False
return np.r_[2, 2*np.nonzero(mask)[0][1::]+1]