简体   繁体   中英

python - finding circular prime number

I am trying trying to find the number of circular primes from a given limit. The prime(x) will return whether a number is a prime or not. The rotations() will return a list of rotated numbers. Lastly, prime_count() will output the total amount of circular primes based on the given limit. Both prime() and rotations() gave me the correct output; however, prime_count() is not incrementing like it should. Any ideas on what i did wrong?

def prime(number): #return true or false
    return all(number% i for i in range(2,number))

def rotations(num): #rotating number and return list
    list = []
    m = str(num)
    counter = 0 
    while counter < len(str(num)):
        m=m[1:] + m[0]
        list.append(int(m))
        counter+=1
    list1=sorted(list,key=int)
    return list1

def prime_count(limit): #return numbers of circular primes from given limit
    counter = 0 

    for i in range(1,limit+1):
        a=rotations(i)
        for j in a:
            if j == prime(j): 
                counter+=1 

    return counter

print(prime_count(100))

There are a few problems with your code:

  1. Your prime function has a bug:

     In [8]: prime(1) Out[8]: True 

    It erroneously returns True for any number less than 2 due to range(2, n) being empty and any([]) == True .

  2. prime_count should be counting the total number of circular primes below limit . prime(j) returns a boolean, but you check j == prime(j) , which can only be true if j is zero or one, which definitely isn't what you want. Try creating an is_circular_prime function that takes in an integer n and returns whether or not the prime is circular. Then, prime_count becomes easy to write.

This is the heart of the problem:

a=rotations(i)
for j in a:
    if j == prime(j): 
        counter+=1

You're counting the wrong thing (eg 13 counts twice independent of 31 counting twice) and you're comparing the wrong thing (numbers against booleans.) The problem is simpler than you're making it. Rearranging your code:

def prime(number):
    return number > 1 and all(number % i != 0 for i in range(2, number))

def rotations(num):
    rotated = []

    m = str(num)

    for _ in m:
        rotated.append(int(m))
        m = m[1:] + m[0]

    return rotated

def prime_count(limit):
    counter = 0 

    for number in range(1, limit + 1):

        if all(prime(rotation) for rotation in rotations(number)): 
            counter += 1 

    return counter

print(prime_count(100))

Note that you don't need to sort the rotations for this purpose. Also list , or any other Python built-in function, is a bad name for a variable.

Problem may be here:

 for i in range(1,limit+1):
        a=rotations(i)
        for j in a:
            if j == prime(j):   # Prime will return True or False, comapring with J will cause it False ,except when J = 1 
                counter+=1 

Changing it to prime(j)

    for i in range(2,number//2):
        if(number%i==0):
            return False 
    return True  

def rotations(num):
    count=0 
    rotation_lst=[]
    num=str(num)
    while(count<len(num)):
        num=num[1:]+num[0]
        count+=1 
        rotation_lst.append(int(num))
    rotation_lst1=sorted(rotation_lst,key=int)
    return rotation_lst1

def get_circular_prime_count(limit):
    count=0
    for i in range(1,limit+1):
        a=rotations(i)

        for j in a:
            if(check_prime(j)):
                count+=1 
    return count 
print(get_circular_prime_count(1000) ```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM