-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprime.py
More file actions
executable file
·55 lines (47 loc) · 1.25 KB
/
prime.py
File metadata and controls
executable file
·55 lines (47 loc) · 1.25 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
#!/usr/bin/env python
#
# Calcula un determinado numero de numeros primos
# Despachando procesos por core.
#
#
import sys
import time
import math
from multiprocessing import Process, Pool, cpu_count
#Check if the user give the number of processes to dispatch
if len(sys.argv) >= 2:
process_to_disppatch = int(sys.argv[1])
if not process_to_disppatch:
process_to_disppatch = cpu_count()
else:
process_to_disppatch = 1
print "Process to dispatch: %d"%process_to_disppatch
def is_prime(number):
fnumber = float(number)
prime = True
root = math.sqrt(number)
if math.ceil(root) % 1:
prime = False
else:
maxi = math.ceil(root)
i = 2
while i <= maxi:
if not number % i:
prime = False
break
i += 1
#print prime, number
return prime, number
po = Pool(process_to_disppatch)
c = time.time()
results = po.map(is_prime, (k for k in xrange(3,200)))
print "Processing time was: ", time.time() - c
# remove if you want to see the prime numbers :-)
#sys.exit()
#print len(results)
results.sort(None, lambda x: x[1])
print repr([v for k,v in results if k])
for prime, number in results:
#print prime, number
if prime:
print number, prime