How to check if a number is circular prime (cyclically rotate left will also be prime) in Python

1 Answer

0 votes
from sympy import isprime
 
def cyclically_rotate_left(n) :
    m = n
    p = 1
    
    while (m >= 10) :
        p *= 10
        m /= 10
    
    return int((m + 10 * (n % p)))

def is_circular_prime(n) :
    if (not isprime(n)) :
        return False
        
    rotated_n = cyclically_rotate_left(n)

    while (rotated_n != n) :
        if (rotated_n < n or not isprime(rotated_n)) :
            return False
        rotated_n = cyclically_rotate_left(rotated_n)
        
    return True

        
print(is_circular_prime(3779))




'''
run:

True

'''

 



answered Mar 24, 2023 by avibootz
...