Solution to problem 35
This commit is contained in:
parent
ee4185b3d8
commit
1d67f5c191
45
src/Python/Problem035.py
Normal file
45
src/Python/Problem035.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#!/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):
|
||||||
|
for j in circular_number(i):
|
||||||
|
if not is_prime(j):
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
circular_primes.append(i)
|
||||||
|
|
||||||
|
return len(circular_primes)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
print(f"Result for Problem 35: {compute()}")
|
Loading…
x
Reference in New Issue
Block a user