Last active
June 4, 2018 17:28
-
-
Save zmonoid/4516efcfb15e3e572e2f96bc2b5cceaa to your computer and use it in GitHub Desktop.
This is to solve one of euler project's problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_prime_till(d): | |
assert d > 10 | |
x = [True] * (d+1) | |
x[0] = False | |
x[1] = False | |
last_prime = 2 | |
while True: | |
k = last_prime*2 | |
while k < d+1: | |
x[k] = False | |
k += last_prime | |
for i in range(last_prime+1, d): | |
if x[i]: | |
last_prime = i | |
break | |
if last_prime * last_prime > d: | |
break | |
return [idx for idx, i in enumerate(x) if i] | |
def find_quadratic(inputs): | |
a, b, primes = inputs | |
max_n = max(abs(a), abs(b)) | |
cons_primes = [] | |
for n in range(max_n+1): | |
k = n*n + a*n + b | |
if k in primes: | |
cons_primes.append(k) | |
else: | |
break | |
return len(cons_primes) | |
import time | |
from multiprocessing import Pool | |
now = time.time() | |
primes = find_prime_till(1000*1000) | |
cc = [(a, b, primes) for a in range(-100, 100) for b in range(-100, 100)] | |
print(time.time() - now, "Time spend on calculating primes") | |
w = time.time() | |
with Pool(processes=24) as pool: | |
out = pool.map(find_quadratic, cc) | |
print(time.time() - now, "Time spend on find_quadratic") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment