Ruff in pre-commit
This commit is contained in:
50
src/project_euler_python/problems_001_050/Problem035.py
Normal file
50
src/project_euler_python/problems_001_050/Problem035.py
Normal file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Created on 02 Apr 2021
|
||||
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for problem 035 of Project Euler
|
||||
https://projecteuler.net/problem=35
|
||||
"""
|
||||
|
||||
from project_euler_python.utils import is_prime, timeit
|
||||
|
||||
|
||||
def circular_number(number):
|
||||
num_str = str(number)
|
||||
ans = []
|
||||
for i in range(len(num_str)):
|
||||
ans.append(int(num_str[i:] + num_str[:i]))
|
||||
return ans
|
||||
|
||||
|
||||
@timeit("Problem 035")
|
||||
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?
|
||||
"""
|
||||
|
||||
ans = []
|
||||
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:
|
||||
ans.append(i)
|
||||
|
||||
return len(ans)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(f"Result for Problem 035: {compute()}")
|
||||
Reference in New Issue
Block a user