Project Euler #35: Circular primes

  • + 0 comments

    100 points.

    lookup=[0]*1000001
    sieve=[False,True,False,True]*250001
    for i in range(3,1001,2):
        if sieve[i]:
            for j in range(i*i,1000001,i):
                sieve[j]=False
    sieve[1],sieve[2]=False,True
    for p in range(2,1000001):
        if sieve[p]:
            if len(str(p))==0:
                lookup[p]+=p
            else:
                for i in range(1,len(str(p))):
                    c=int(str(p)[i:]+str(p)[:i])
                    if not sieve[c]:
                        break
                else:
                    lookup[p]+=p             
        lookup[p]+=lookup[p-1]
    
    n=int(input().strip())
    print(lookup[n])