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