Solution to problem 60 in Python
This commit is contained in:
parent
037f4c0f8d
commit
6157818ad0
@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Created on 13 Oct 2021
|
||||
Created on 04 Dic 2021
|
||||
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
@ -9,8 +9,7 @@ Solution for problem 60 of Project Euler
|
||||
https://projecteuler.net/problem=60
|
||||
"""
|
||||
|
||||
from itertools import permutations
|
||||
from utils import timeit, list_primes
|
||||
from utils import timeit, list_primes, is_prime
|
||||
|
||||
|
||||
@timeit("Problem 60")
|
||||
@ -28,15 +27,26 @@ def compute():
|
||||
|
||||
primes_list = list_primes(10_000)
|
||||
|
||||
for nums in permutations(primes_list, 5):
|
||||
for n1, n2 in permutations(nums, 2):
|
||||
if int(str(n1) + str(n2)) not in primes_list:
|
||||
break
|
||||
primes = sum(nums)
|
||||
print(nums)
|
||||
# break
|
||||
def solve_backtrack(max_chain_length, chain):
|
||||
for p in primes_list[1:]:
|
||||
if p not in chain and is_concatenable(chain, p):
|
||||
chain.append(p)
|
||||
if len(chain) == max_chain_length:
|
||||
return chain
|
||||
solve_backtrack(max_chain_length, chain)
|
||||
chain.pop()
|
||||
return chain
|
||||
|
||||
return primes
|
||||
def is_concatenable(chain, candidate):
|
||||
for n in chain:
|
||||
if not is_prime(int(str(n) + str(candidate))):
|
||||
return False
|
||||
if not is_prime(int(str(candidate) + str(n))):
|
||||
return False
|
||||
return True
|
||||
|
||||
result = solve_backtrack(5, [])
|
||||
return sum(result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
x
Reference in New Issue
Block a user