Solution to problem 37
This commit is contained in:
parent
4985501d1a
commit
2213718a30
@ -9,10 +9,15 @@ Solution for problem 37 of Project Euler
|
||||
https://projecteuler.net/problem=37
|
||||
"""
|
||||
|
||||
from itertools import product
|
||||
from utils import timeit, is_prime
|
||||
from utils import timeit, list_primes, is_prime
|
||||
|
||||
def is_truncatable_prime(number):
|
||||
num_str = str(number)
|
||||
for i in range(1, len(num_str)):
|
||||
if not is_prime(int(num_str[i:])) or not is_prime(int(num_str[:-i])):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@timeit("Problem 37")
|
||||
def compute():
|
||||
@ -27,28 +32,12 @@ def compute():
|
||||
|
||||
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
|
||||
"""
|
||||
|
||||
primes, ans = [], 0
|
||||
for i in range(2,7):
|
||||
candidates = product([1,2,3,5,7,9], repeat=i)
|
||||
for t in candidates:
|
||||
primes.append(''.join([str(d) for d in t]))
|
||||
|
||||
for p in primes:
|
||||
trunc = True
|
||||
if not is_prime(int(p)):
|
||||
trunc = False
|
||||
continue
|
||||
for i in range(1, len(p)):
|
||||
if not is_prime(int(p[i:])):
|
||||
trunc = False
|
||||
break
|
||||
if not is_prime(int(p[:i])):
|
||||
trunc = False
|
||||
break
|
||||
if trunc:
|
||||
ans += int(p)
|
||||
|
||||
ans = 0
|
||||
primes = list_primes(1_000_000)
|
||||
# Statement of the problem says this
|
||||
for number in primes[4:]:
|
||||
if is_truncatable_prime(number):
|
||||
ans += number
|
||||
return ans
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user