/
findOEIS.py
133 lines (110 loc) · 2.89 KB
/
findOEIS.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import PyMachine
def mapSeq(fname, maxi=1000000000):
d = {}
f = open(fname)
i = 0
for l in f:
i += 1
if i % 100000 == 0:
print i
if i >= maxi:
break
nr, depth, program, seq = l.split(' ', 3)
d[seq.strip()] = (nr,depth,program)
f.close()
return d
def mapOEIS(fname='stripped'):
d = {}
f = open(fname)
for l in f:
l.strip()
if l.startswith('#'):
continue
seqs = l.split(',')
s = ' '.join(seqs[1:11])
if s not in d:
d[s] = []
d[s].append(seqs[0].strip())
f.close()
return d
def checkSeq(fname, moe):
dpth = [0]*40
f = open(fname)
cnt = 0
for l in f:
nr, depth, program, seq = l.split(' ', 3)
seq = seq.strip()
if seq in moe:
#print seq, ':', nr,depth,program, moe[seq]
dpth[int(depth)] += 1
cnt += 1
f.close()
print cnt
return dpth
def findPrimes(fname, allowedMiss=0):
ps = []
f = open(fname)
i = 0
j = 0
for l in f:
i += 1
nr, depth, program, seq = l.split(' ', 3)
seq = seq.strip()
xs = [int(x) for x in seq.split()]
if len(set(xs)) != 10: continue
good = True
for x in xs:
if x < 1:
good = False
break
if not good: continue
j += 1
misses = 0
for x in xs:
if not PyMachine.checkPrime(x):
misses += 1
if misses > allowedMiss:
break
if misses <= allowedMiss:
print misses, xs, nr, depth, program
ps.append((program, depth))
print i, j
f.close()
return ps
def isPrime(n):
'''check if integer n is a prime'''
if n == 1:
return True
if n == 2:
return True
if not n & 1:
return False
# range starts with 3 and only needs to go up the squareroot of n
# for all odd numbers
for x in xrange(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True
def extendPotentialPrimes(fname, maxIterations, allowedMiss):
ps = findPrimes(fname, allowedMiss)
pm = PyMachine.PyMachine(maxIterations, 1, maxIterations, 0, maxIterations)
extended = []
for program, plen in ps:
pm.load(long(program), int(plen))
pm.execute()
oldp = -1
outputs = []
nrisp = 0
v = pm.getOutput()
print [x for x in v]
for p in v:
if p <= oldp:
break
isp = PyMachine.checkPrime(p)
nrisp += isp
outputs.append((p, isp))
if nrisp:
extended.append((float(nrisp) / len(outputs), nrisp, len(outputs), outputs, program, plen))
extended.sort()
extended.reverse()
return extended