48 lines
1.1 KiB
Python
48 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Created on 02 Apr 2021
|
|
|
|
@author: David Doblas Jiménez
|
|
@email: daviddoji@pm.me
|
|
|
|
Solution for problem 35 of Project Euler
|
|
https://projecteuler.net/problem=35
|
|
"""
|
|
|
|
from utils import timeit, is_prime
|
|
|
|
def circular_number(n):
|
|
n = str(n)
|
|
result = []
|
|
for i in range(len(n)):
|
|
result.append(int(n[i:] + n[:i]))
|
|
return result
|
|
|
|
@timeit("Problem 35")
|
|
def compute():
|
|
"""
|
|
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?
|
|
"""
|
|
circular_primes = []
|
|
for i in range(2, 1_000_000):
|
|
if is_prime(i):
|
|
all_primes = True
|
|
for j in circular_number(i):
|
|
if not is_prime(j):
|
|
all_primes = False
|
|
break
|
|
if all_primes:
|
|
circular_primes.append(i)
|
|
|
|
return len(circular_primes)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print(f"Result for Problem 35: {compute()}") |