Created
December 18, 2011 16:55
-
-
Save jakedobkin/1493918 to your computer and use it in GitHub Desktop.
Euler 60- Kind of a Monster Brute
This file contains hidden or 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
| # set up prime sieve to n | |
| n=10000 | |
| myPrimes = [True]*(n+1) | |
| last = 0 | |
| for i in range (2,n): | |
| if myPrimes[i] == True: | |
| j = 2*i | |
| while j<=n: | |
| myPrimes[j]=False | |
| j=j+i | |
| def is_prime(n): | |
| if n == 2 or n == 3: return True | |
| if n < 2 or n%2 == 0: return False | |
| if n < 9: return True | |
| if n%3 == 0: return False | |
| r = int(n**.5) | |
| f = 5 | |
| while f <= r: | |
| if n%f == 0: return False | |
| if n%(f+2) == 0: return False | |
| f +=6 | |
| return True | |
| def concatable(x,y): | |
| if is_prime(int(str(x)+str(y))) == True and is_prime(int(str(y)+str(x))) == True: | |
| return True | |
| else: | |
| return False | |
| # dump sieve to list | |
| primelist = [] | |
| for k in range (2,n): | |
| if myPrimes[k] == True: | |
| primelist.append(k) | |
| # general approach is start with a prime and keep adding concatables until you get 5 | |
| # i tried this with starts 3,5,7,11, and then 13- 13 was first to win | |
| # it works in about 40s | |
| start = 13 | |
| for p in range (primelist.index(start)+1,len(primelist)): | |
| list=[start] | |
| list.append(primelist[p]) | |
| for l in range (primelist.index(start)+2,len(primelist)): | |
| for o in range (0, len(list)): | |
| if concatable(primelist[l],list[o])== False or primelist[l]<list[o]: | |
| break | |
| if concatable(primelist[l],list[o]) == True and o == len(list)-1: | |
| list.append(primelist[l]) | |
| if len(list)==5: | |
| print list,sum(list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment