Circular prime
from math import sqrt;
from itertools import count, islice
def is_circular_prime(n):
"""
Function checks if a number is circular prime.
I guess the issue was with the way I was rotating the number.
"""
backup = n
n = ((n%10) * (10**(len(str(n))-1))) + n//10
while n != backup:
if not is_prime(n):
return False
# assumes this is where the issue was.
n = ((n%10) * (10**(len(str(n))-1))) + n//10
return True
def is_prime(n):
"""Hope using my own code from years ago is okay.
source: https://github.com/soorajiam/Project-Euler/blob/master/3.%20Largest%20prime%20factor.ipynb
"""
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
def count_circular_primes(n):
"""
iterates over all possible numbers and checks if they are circular primes.
"""
count = 0
for i in range(2, n):
# checks that the number is prime and circular prime.
if is_prime(i) and is_circular_prime(i):
print(i)
count += 1
print(count)
return count
print(count_circular_primes(100))Last updated