Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,917 questions

51,850 answers

573 users

How to find the first 18 circular prime numbers (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

        
n = 2
i = 0
while (i < 18) :
    if (is_circular_prime(n)) :
        if (i > 0) :
            print(", ", end ="")
        print(n, end ="")
        i += 1
    n += 1        




'''
run:

2, 3, 5, 7, 11, 13, 17, 37, 79, 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939

'''

 



answered Mar 24, 2023 by avibootz
...