-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp049.py
More file actions
executable file
·70 lines (62 loc) · 1.97 KB
/
p049.py
File metadata and controls
executable file
·70 lines (62 loc) · 1.97 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
#
#There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.
#
#What 12-digit number do you form by concatenating the three terms in this sequence?
import logging
from prime import PrimeNumberPool
def IsPermute(a, b):
da = set()
db = set()
while (a > 0):
da.add(a%10)
a = a//10
while (b > 0):
db.add(b%10)
b = b//10
return da == db
def IsArithSeq3(q):
if (len(q) == 3):
if (q[1]-q[0] == q[2] - q[1]):
return q
else:
return 0
for i in range(len(q)-2):
for j in range(i+1, len(q)-1):
for k in range(j+1, len(q)):
nq = [q[i], q[j], q[k]]
if (IsArithSeq3(nq)):
return nq
return 0
def main(args):
prime = PrimeNumberPool()
for i in range(10000):
prime.NewPrime()
prime_num4 = []
for p in prime.numbers:
if (p > 1000 and p < 10000):
prime_num4.append(p)
pn4_perm = {}
pn4_ps = set()
for i in range(len(prime_num4)-1):
p = prime_num4[i]
if (p in pn4_ps):
continue
for j in range(i+1, len(prime_num4)):
q = prime_num4[j]
if (q in pn4_ps):
continue
if (IsPermute(p,q)):
pn4_ps.add(p)
pn4_ps.add(q)
if (p in pn4_perm):
pn4_perm[p].append(q)
else:
pn4_perm[p] = [p, q]
for p in list(pn4_perm.keys()):
ps = pn4_perm[p]
if (len(ps) < 3):
continue
if (IsArithSeq3(ps)):
logging.info(ps)
logging.info(IsArithSeq3(ps))