Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - finding circular prime number

Tags:

python

primes

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))
like image 595
begincoding123 Avatar asked Feb 22 '26 00:02

begincoding123


2 Answers

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.

like image 84
Blender Avatar answered Feb 23 '26 13:02

Blender


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.

like image 39
cdlane Avatar answered Feb 23 '26 13:02

cdlane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!