边长为n时,四个角落的数分别是$n^2$、$n^2-(n-1)$、$n^2-2(n-1)$、$n^2-3(n-1)$,因此只需不断测试和统计后三项是否为素数即可,结果是$26241$。
target=100000
d=[0]*target
n=2
while n < target:
for i in range(n+n,target,n):
d[i]=d[i]+1
n=n+1
while n < target and d[n]>0:
n=n+1
primeList=[k for k in range(2,target) if d[k]==0]
primeSet=set(primeList)
def isPrime(m):
if m <=100000:
return m in primeSet
else:
i=0
while primeList[i]*primeList[i] <= m and m%primeList[i]:
i+=1
return m%primeList[i]>0
c=8
d=13
n=7
while 10*c>=d:
n+=2
d+=4
c+=(isPrime(n*n-n+1)+isPrime(n*n-2*n+2)+isPrime(n*n-3*n+3))
print n
|