-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem60.py
More file actions
38 lines (33 loc) · 1.18 KB
/
problem60.py
File metadata and controls
38 lines (33 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Prime pair sets
Project Euler Problem #60
by Muaz Siddiqui
The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and
concatenating them in any order the result will always be prime. For example,
taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792,
represents the lowest sum for a set of four primes with this property.
Find the lowest sum for a set of five primes for which any two primes concatenate
to produce another prime.
"""
from euler_helpers import timeit, is_prime, primes_to
import itertools
prime_list = primes_to(8400)
def is_prime_set(primes):
return all(is_prime(int(str(prime[0]) + str(prime[1]))) for prime in itertools.permutations(primes, 2))
def prime_pairs(primes, length):
if len(primes) == length:
return primes
for prime in prime_list:
check = primes + [prime]
if prime > primes[-1] and is_prime_set(check):
next = prime_pairs(check, length)
if next:
return next
return 0
@timeit
def answer():
while True:
pairs = prime_pairs([prime_list.pop(0)], 5)
if pairs:
print(pairs)
return sum(pairs)