Last active
August 29, 2015 14:21
-
-
Save GnsP/e2cc52880d0bc08457cf to your computer and use it in GitHub Desktop.
A project euler problem
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 arithmetic sequence 1487, 4817, 8147, in which each of the terms increase by | |
3330 is unusual in 2 ways. (i) each of the terms are prime (ii) there are permutations | |
of each other. | |
There is one more 4 digit sequence like this. Find the 12digit number formed by | |
concatenating them. | |
""" | |
######################################################################################## | |
""" | |
EDIT: | |
When the problem says about another 4 digit sequence like the given one, it also means | |
that the difference between the terms is 3330. (This actually simplifies the matter a lot). | |
I found this out after generating the 2nd sequence the normal way. When I saw the 2nd | |
sequnce, I understood that it meant the difference was to be 3330 and I could have done it | |
more easily. :D | |
""" | |
LIM = 9999 # The number upto which we search | |
def seive(n): # Seive of Eratosthenes to find all primes till 9999 | |
l=[True for i in range(n+1)] | |
l[0:2] = [False,False] | |
primes = [] | |
lim = int(n**0.5)+2 | |
for i in range(2,lim): | |
if l[i]: | |
primes.append(i) | |
j=i*2 | |
while j<=n: | |
l[j]=False | |
j+=i | |
for i in range(lim, n+1): | |
if l[i]: | |
primes.append(i) | |
return [l,primes] | |
[Seive,Primes] = seive(LIM) # Run the seive and generate the list of primes | |
def find_ap_seqs(): # Find all AP sequences in the generated list of primes | |
res = [] | |
for i in range(len(Primes)-2): | |
for j in Primes[i+1:-1]: | |
diff = j-Primes[i] | |
if j+diff <= LIM: | |
if Seive[j+diff]: | |
res.append([Primes[i],j,j+diff]) | |
return res | |
def find_ap_seqs_new(): | |
"""This new function is written to speed up the search considering the condition | |
that the difference between two terms in the sequence is 3330""" | |
res = [] | |
for i in Primes[:-2]: | |
if i+3330 <= LIM: | |
if Seive[i+3330]: | |
if i+6660 <= LIM: | |
if Seive[i+6660]: | |
res.append([i,i+3330,i+6660]) | |
return res | |
AP = find_ap_seqs_new() | |
def isPerm(a,b): # Check if two numbers are permutations of each other | |
return sorted(str(a))==sorted(str(b)) | |
def check_permutations(): # Check the list of sequences to find the sequence which satisfies the permutation condition | |
res = [] | |
for seq in AP: | |
if isPerm(seq[0],seq[1]) and isPerm(seq[0], seq[2]): | |
res.append(seq) | |
return res | |
Final = check_permutations() | |
## Final: [[1487, 4817, 8147], [2969, 6299, 9629]] | |
del Final[Final.index([1487,4817,8147])] | |
print(''.join([str(x) for x in Final[0]])) | |
# Output : 296962999629 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment