target=1000000 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 print 2*sum((p*p+11)/6-((p*p+11)%6==0)-(p*p+13)/8+((p*p+13)%8==0) for p in range(2,target) if d[p]==0)