Project Euler Problem 035

Statement

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

Solution

Straight-forward resolution

from CommonFunctions import find_primes_less_than
 
primes = set(find_primes_less_than(1000000))
 
def is_circular_prime(n):
    for i in range(0, len(str(n)) + 1):
        n = int(str(n)[-1] + str(n)[:-1])
        if n not in primes:
            return False
    return True
 
if __name__ == '__main__':
    result = len([i for i in primes if is_circular_prime(i)])
    print("The result is:", result)

The Python file is available for download here.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License