forked from tdamdouni/Pythonista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimes.py
40 lines (35 loc) · 978 Bytes
/
Primes.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
# Demo of how to generate a list of prime numbers and cache them
# in a data file for fast access (using the marshal module).
def gen_primes(n):
# Source: http://code.activestate.com/recipes/366178-a-fast-prime-number-list-generator/#c19
s = range(0, n+1)
s[1] = 0
bottom = 2
top = n // bottom
while (bottom * bottom <= n):
while (bottom <= top):
if s[top]:
s[top * bottom] = 0
top -= 1
bottom += 1
top = n // bottom
return [x for x in s if x]
def load_primes(n):
import marshal
filename = 'primes_' + str(n)
try:
# Load cached primes file:
with open(filename, 'r') as f:
return marshal.load(f)
except:
# Generate primes and save to file:
primes_list = gen_primes(n)
with open(filename, 'w') as f:
marshal.dump(primes_list, f)
return primes_list
def main():
primes = load_primes(104729) #Happens to be the 10,000th prime number
print primes
print len(primes), 'prime numbers loaded.'
if __name__ == '__main__':
main()