Database Reference
In-Depth Information
Listing 12.7 Identifying prime numbers
# prime_finder.py: nonparallel prime-number finder
import numpy as np
# Generate some random large integers
np.random.seed(seed=12345)
possible_primes = np.random.random_integers(1000000, 20000000,
1000).tolist()
def prime_check(num):
# Check all integers between 2 and num
for test in range(2, num):
# Divisible by test number?
if num % test == 0:
return False
return True
def find_primes(potential_list):
for number in potential_list:
print '%d is prime? %r' % (number, prime_check(number))
find_primes(possible_primes)
time ipython prime_finder.py
17645405 is prime? False
19173348 is prime? False
3993577 is prime? False
7164478 is prime? False
4555874 is prime? False
8708189 is prime? False
14679361 is prime? True
16363190 is prime? False
5246897 is prime? False
9002190 is prime? False
# ... etc ...
1667625154 is prime? False
time output:
real 0m9.249s
Even on a fast workstation, running the code in Listing 12.7 will take a few sec-
onds. On a fairly new laptop, the example above took over nine seconds to run over
only 1000 large numbers. Although the problem is partially CPU bound, this data
challenge is one that can easily be made to run in parallel, so it makes more sense to
farm this out to multiple processing cores.
Even if you don't have a cluster of machines available, you can observe some per-
formance gains using a local machine with a multicore processor. In the following
 
Search WWH ::




Custom Search