解决了此问题链接https://projecteuler.net/problem=27之后,我找到了解决该问题的另一种方法:
import itertools , eulerlib
def compute():
ans = max(((a, b) for a in range(-999, 1000) for b in range(2, 1000)),
key=count_consecutive_primes)
return str(ans[0] * ans[1])
def count_consecutive_primes(ab):
a, b = ab
for i in itertools.count():
n = i * i + i * a + b
if not is_prime(n):
return i
isprimecache = eulerlib.list_primality(1000)
def is_prime(n):
if n < 0:
return False
elif n < len(isprimecache):
return isprimecache[n]
else:
return eulerlib.is_prime(n)
if __name__ == "__main__":
print(compute())
当我运行它时,我收到此消息错误: AttributeError:“模块”对象没有属性“ list_primality”。 实际上,我已经安装了euerlib