Question

Problem Statement :

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

My Problem I have checked through all the code and found that the binary search function is giving a return 1 statement as the output print success. But nothing is added to the final list. Please Help

Program in Python :

from time import time
start = time()
LIMIT = 1000000   # largest limit of the prime numbers
prima = []   # list of primes later to be filled by primes function

# binary search function
def Bsearch(lsta,low,high,search):
        if low>high:
                return -1
        else:
            mid = int((low+high)/2)
            if search<lsta[mid]:
                Bsearch(lsta,low,mid-1,search)
            elif search>lsta[mid]:
                Bsearch(lsta,mid+1,high,search)
            elif search==lsta[mid]:
                print("Success!")
                return 1

# prime number generating function
# uses sieve of Era** algorithm
# produces correct result tested
def primes(LIMIT):
    lsta = {}  # temporaty empty dictionary
    for i in range(2,LIMIT):
        lsta[i] = 1
    for i in range(2,LIMIT):
        for j in range(i,LIMIT):
            if i*j>LIMIT:
                break
            lsta[i*j] = 0
    for i in range(2,LIMIT):
        if(lsta[i]==1):
            prima.append(i)
primes(LIMIT)

final = []
for item in prima:
    x = int(str(item)[::-1])
    # real problem here the following statement not inserting any value in final list
    if(Bsearch(prima,0,len(prima)-1,x)):
        print("Hello!")
        print(final)
        final.append(item)
print(final)
Was it helpful?

Solution

Quickly generate prime numbers and list out Circular Prime numbers

def primes(max_n):
    numbers = range(3, max_n+1, 2)
    half = (max_n)//2
    initial = 4
    for step in xrange(3, max_n+1, 2):
        for i in xrange(initial, half, step):
            numbers[i-1] = 0
        initial += 2*(step+1)

        if initial > half:
            return [2] + filter(None, numbers)


def rotate(S_list):
    S=[]
    for i in range(len(S_list)):
        S.append(int(S_list[i:]+S_list[:i]))
    return set(S)

def circularPrime(limit):
    All_primes_in_limit = primes(limit)
    circular_prime=[]
    reject_list=['0','2','4','5','6','8']
    All_primes_in_limit=[i for i in All_primes_in_limit if not any(j in reject_list for j in set(str(i)))]
    while All_primes_in_limit:
        ShufleSet=rotate(str(All_primes_in_limit[-1]))
        PrimesSet=set(All_primes_in_limit)
        if not ShufleSet -  PrimesSet:
            circular_prime+=list(ShufleSet)
        All_primes_in_limit=list(PrimesSet-ShufleSet)
    circular_prime.sort()
    return circular_prime


#for limit value 1000000
print circularPrime(1000000)

This is the fastest possible algorithm for circular prime number listing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top