python - finding circular prime number

久未见 提交于 2020-06-27 19:44:11

问题


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))

回答1:


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.




回答2:


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 (e.g. 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.




回答3:


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)




回答4:


    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) ```


来源:https://stackoverflow.com/questions/49663638/python-finding-circular-prime-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!