-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.py
43 lines (32 loc) · 947 Bytes
/
parallel.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
# https://projecteuler.net/problem=10
import math
import unittest
from multiprocessing import Pool, Process
def is_prime(m):
acc = 0
for n in range(1, m+1):
acc += 1 if m%n == 0 else 0
if acc > 2:
return False
return True
def get_primes(nth_start, nth_end):
primes = []
for n in range(nth_start, nth_end+1):
if is_prime(n):
primes.append(n)
print(len(primes))
return primes
def summation_of_primes(nth_start, nth_end):
print("starting: ", nth_start, nth_end)
return sum(get_primes(nth_start, nth_end))
def pool_fn(fn):
p = Pool(5)
res = p.starmap(fn, [(2, 1000000), (1000000, 2000000)])
print("finish")
print(res)
return sum(res)
class TestSmallestMultiple(unittest.TestCase):
def test_1_1(self):
self.assertEqual(pool_fn(summation_of_primes), 123)
if __name__ == '__main__':
unittest.main()