def primes(n): """ Nice way to calculate primes. Should be fast. """ l = range(2, n + 1) _l = [] while True: p = l[0] if p * p > n: return _l + l l = [i for i in l if i % p != 0] _l.append(p) def produce_prime(a, b, n, primes): x = n*n + a*n + b return x in primes def f_027(): """ n^2 + a*n + b 1) b must be prime """ p6 = set(primes(1000000)) p3 = primes(1000) options = [(a, b) for a in range(1, 1000, 2) for b in p3] print(len(options)) for n in range(100): options = [(a, b) for a, b in options if produce_prime(a, b, n, p6)] print(options) print(len(options)) f_027()