-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbload.py
65 lines (52 loc) · 1.56 KB
/
dbload.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
60
61
62
63
64
#!/usr/bin/python
from utils import ndivisors, trial_division, FastPrimeSieve
from math import sqrt
import cPickle, multiprocessing
import gzip
from pymongo import Connection
global progress
progress = 0
def worker_load(data):
con = Connection('localhost')
db = con['integers']
col = db['misc']
col.insert(data)
con.disconnect()
def cb(result):
global progress
progress += result
if progress % int(1e5) == 0:
print "Progress: %f" % ((float(progress) / 1e9)*100)
def worker_file(a,b, primes):
with gzip.open('dump.bin', 'ab', compresslevel = 3) as fp:
buf = []
for n in xrange(a, b):
prime_factors = trial_division(n, primes)
num_factors = len(prime_factors)
data = {'n' : n,
'factors' : prime_factors,
'ndivisors' : ndivisors(n, prime_factors),
'nfactors' : num_factors,
'max_factor': max(prime_factors),
'primality' : num_factors == 1}
buf.append(data)
cPickle.dump(buf, fp, protocol = 2)
return (b-a)
def main(N, jobsize):
global progress
print "Generating primes"
maxp = int(sqrt(N)) + 1
primes = FastPrimeSieve(maxp)
print "Computing factors"
pool = multiprocessing.Pool()
buf = []
a = 2
b = a + jobsize
while a <= N:
pool.apply_async(worker_file, [a, b, primes], callback=cb)
a = b
b = a + jobsize
pool.close()
pool.join()
if __name__ == "__main__":
main(int(1e9), 5000)