Last active
August 29, 2015 13:56
-
-
Save dfeng/8857215 to your computer and use it in GitHub Desktop.
Derek's 60
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. | |
def primes(n): | |
ret = list() | |
multiples = set() | |
for i in xrange(2, n+1): | |
if i not in multiples: | |
ret.append(i) | |
multiples.update(xrange(i*i, n+1, i)) | |
return ret | |
PRIMES = primes(10**8) | |
PRIMES_set = set(PRIMES) | |
group_list = list() | |
def check(a,b): | |
return (int(str(a)+str(b)) in PRIMES_set) and (int(str(b)+str(a)) in PRIMES_set) | |
def solve(): | |
for p in PRIMES: | |
for group in group_list: | |
if all([check(prime, p) for prime in group]): | |
g = list(group) # DAMN PYTHON | |
g.append(p) | |
group_list.append(g) | |
if len(g) == 5: | |
return g, sum(g) | |
group_list.append([p]) | |
print solve() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment