summaryrefslogtreecommitdiff
path: root/primes.py
diff options
context:
space:
mode:
Diffstat (limited to 'primes.py')
-rw-r--r--primes.py19
1 files changed, 7 insertions, 12 deletions
diff --git a/primes.py b/primes.py
index aff6697..7e83ea0 100644
--- a/primes.py
+++ b/primes.py
@@ -10,30 +10,25 @@ PRIMES = [
115797848077099]
def is_prime(n):
- n = abs(n)
- i = 2
- while i <= math.sqrt(n):
+ if n % 2 == 0:
+ return False
+
+ sqrt_n = int(math.floor(math.sqrt(n)))
+ for i in range(3, sqrt_n + 1, 2):
if n % i == 0:
return False
- i += 1
return True
def sequential():
return list(map(is_prime, PRIMES))
def with_process_pool_executor():
- executor = futures.ProcessPoolExecutor(10)
- try:
+ with futures.ProcessPoolExecutor(10) as executor:
return list(executor.map(is_prime, PRIMES))
- finally:
- executor.shutdown()
def with_thread_pool_executor():
- executor = futures.ThreadPoolExecutor(10)
- try:
+ with futures.ThreadPoolExecutor(10) as executor:
return list(executor.map(is_prime, PRIMES))
- finally:
- executor.shutdown()
def main():
for name, fn in [('sequential', sequential),