Adopted new convention from template
This commit is contained in:
parent
a0775260db
commit
3eba4ddab6
@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
Created on 04 Dic 2021
|
||||
|
||||
@ -9,7 +9,31 @@ Solution for problem 60 of Project Euler
|
||||
https://projecteuler.net/problem=60
|
||||
"""
|
||||
|
||||
from utils import timeit, list_primes, is_prime
|
||||
from utils import is_prime, list_primes, timeit
|
||||
|
||||
|
||||
def solve_backtrack(max_chain_length, chain):
|
||||
primes_list = list_primes(10_000)
|
||||
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
@timeit("Problem 60")
|
||||
@ -25,30 +49,9 @@ def compute():
|
||||
concatenate to produce another prime.
|
||||
"""
|
||||
|
||||
primes_list = list_primes(10_000)
|
||||
|
||||
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
|
||||
|
||||
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)
|
||||
ans = solve_backtrack(5, [])
|
||||
return sum(ans)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem 60: {compute()}")
|
||||
print(f"Result for Problem 60 is {compute()}")
|
||||
|
Loading…
x
Reference in New Issue
Block a user