-
Notifications
You must be signed in to change notification settings - Fork 0
/
051.py
237 lines (194 loc) · 3.85 KB
/
051.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#https://projecteuler.net/problem=51
import time
tnow = time.time()
#i gave up and looked up http://www.mathblog.dk/project-euler-51-eight-prime-family/
#more like i started working on this problem 2 months ago..
#miller rabin primarity test
from random import randrange
def check(a, s, d, n):
x = pow(a, d, n)
if x == 1: return True
for i in xrange(s - 1):
if x == n - 1: return True
x = pow(x, 2, n)
return x == n - 1
def miller_rabin(n, k=10):
if n == 2: return True
if not n & 1: return False
s = 0
d = n - 1
while d % 2 == 0:
d >>= 1
s += 1
for i in xrange(k):
a = randrange(2, n - 1)
if not check(a, s, d, n): return False
return True
def makenum(numberlist):
newnum = ""
for i in numberlist:
newnum = newnum+i
altnum = int(newnum)
return altnum
def checknthdigit(num, nlist):
#digits counting from 1s place
numlis = list()
numstr = str(num)
primefam = list()
numindex = len(numstr)
for d in numstr:
numlis.append(d)
#i have to change a number's x location with xrange(10)
for _ in xrange(10):
for m in nlist:
numlis[numindex-m] = str(_)
altnum = makenum(numlis)
if miller_rabin(altnum):
primefam.append(altnum)
return primefam
def isthisayes(num):
numstring = str(num)
nonolist = ("0","2","4","5",'6','8')
if numstring[len(numstring)-1] in nonolist:
return False
else:
return True
"""#---------------------------------------------------------------
#it's like, when doing the checking, it removes other candidates
notcandidate = []
#don't have to count up.. just form the numbers n-digit based
#now need a method to count up
1* 10 -> ones digit place will never give more than 5 primes
*1 1
1** 100 -> anything that has to do with ones place doesn't work
10* 10
1*1 -> even with these cases, numbers that end with 0,2,4,5,6,8 are
never primes
10*0
1*00
1**0
100*0
10*00
1*000
1*0*0
10**0
1**00
1***0
--
1000*0
100*00
10*000
1*0000
100**0
10*0*0
1*00*0
10**00
1*0*00
1**000
10***0
1*0**0
1***00
1**0*0
1****0
"""
# limit = 1000000
# li = [2,3,5]
# counter = 0
# solutions = []
# for _ in xrange(limit/10,limit):
# if isthisayes(_):
# #how to get rid of duplicate searching
# result = checknthdigit(_, li)
# if len(result) > 6 :
# if result not in solutions:
# solutions.append(result)
# print _, "primefam:", result
#_----------___----___---__--___--__-___--__-different approach, get a prime list, and look within that
# limit = 10000000
# data = []
# for i in xrange(100, limit):
# if isthisayes(i) and miller_rabin(i):
# data.append(i)
# print len(data)
# f = open("newfile.txt", "w")
# for stuff in data:
# f.write(str(stuff)+" ")
# f.close()
def checkwithindata(_,nlist):
global data
numlis = list()
numstr = str(_)
primefam = list()
numindex = len(numstr)-1
for d in numstr:
numlis.append(d)
for p in xrange(10):
for m in nlist:
numlis[m] = str(p)
altnum = makenum(numlis)
if miller_rabin(altnum):
primefam.append(altnum)
return primefam
with open("p051_junkprime.txt") as f:
data = f.read()
data = data.split(" ")
beta = []
for i in data:
try:
beta.append(int(i))
except:
continue
data = beta
solutions = []
li = [6]
'''
* all clear
1**000
1***00
'''
digitlength = 6
for i in data:
if i < 10**(digitlength-1):
continue
if i >= 10**digitlength:
break
result = checkwithindata(i, li)
if len(result) > 6 :
if result not in solutions:
solutions.append(result)
print "primefam:", result
"""
10000*0
1000*00
100*000
10*0000
1*00000
1**0000
1*0*000
1*00*00
1*000*0
10**000
10*0*00
10*00*0
100**00
100*0*0
1000**0
1***000
1**0*00
1**00*0
1*0**00
1*0*0*0
1*00**0
10***00
10**0*0
10*0**0
100***0
10****0
1*0***0
1**0**0
1***0*0
1****00
"""
#primefam: [4004509, 4114519, 4224529, 4444549, 4554559, 4774579, 4884589, 4994599]
#primefam: [2090021, 2191121, 2292221, 2494421, 2595521, 2696621, 2898821, 2999921]
print time.time()-tnow, "seconds"