-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallestMultiple.py
324 lines (314 loc) · 8.18 KB
/
SmallestMultiple.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# 2520 is the smallest number that can be divided by each of the
# numbers from 1 to 10 without any remainder.
# What is the smallest positive number that is evenly divisible
# by all of the numbers from 1 to n?
from gmpy2 import mpz
import time
import math
import multiprocessing
def naiti_prostie_menshe(n):
lim = n
# cas particuliers
if lim<7:
if lim<2: return []
if lim<3: return [2]
if lim<5: return [2, 3]
return [2, 3, 5]
# Crible
n = (lim-1)//30
m = n+1
BA = bytearray
prime1 = BA([1])*m
prime7 = BA([1])*m
prime11 = BA([1])*m
prime13 = BA([1])*m
prime17 = BA([1])*m
prime19 = BA([1])*m
prime23 = BA([1])*m
prime29 = BA([1])*m
prime1[0] = 0
i = 0
try:
while 1:
if prime1[i]:
p = 30*i+1
l = i*(p+1)
prime1[l::p] = BA(1+(n-l)//p)
l += i*6
prime7[l::p] = BA(1+(n-l)//p)
l += i*4
prime11[l::p] = BA(1+(n-l)//p)
l += i*2
prime13[l::p] = BA(1+(n-l)//p)
l += i*4
prime17[l::p] = BA(1+(n-l)//p)
l += i*2
prime19[l::p] = BA(1+(n-l)//p)
l += i*4
prime23[l::p] = BA(1+(n-l)//p)
l += i*6
prime29[l::p] = BA(1+(n-l)//p)
if prime7[i]:
p = 30*i+7
l = i*(p+7)+1
prime19[l::p] = BA(1+(n-l)//p)
l += i*4+1
prime17[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime1[l::p] = BA(1+(n-l)//p)
l += i*4
prime29[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime13[l::p] = BA(1+(n-l)//p)
l += i*4+1
prime11[l::p] = BA(1+(n-l)//p)
l += i*6+1
prime23[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime7[l::p] = BA(1+(n-l)//p)
if prime11[i]:
p = 30*i+11
l = i*(p+11)+4
prime1[l::p] = BA(1+(n-l)//p)
l += i*2
prime23[l::p] = BA(1+(n-l)//p)
l += i*4+2
prime7[l::p] = BA(1+(n-l)//p)
l += i*2
prime29[l::p] = BA(1+(n-l)//p)
l += i*4+2
prime13[l::p] = BA(1+(n-l)//p)
l += i*6+2
prime19[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime11[l::p] = BA(1+(n-l)//p)
l += i*6+2
prime17[l::p] = BA(1+(n-l)//p)
if prime13[i]:
p = 30*i+13
l = i*(p+13)+5
prime19[l::p] = BA(1+(n-l)//p)
l += i*4+2
prime11[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime7[l::p] = BA(1+(n-l)//p)
l += i*4+1
prime29[l::p] = BA(1+(n-l)//p)
l += i*6+3
prime17[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime13[l::p] = BA(1+(n-l)//p)
l += i*6+3
prime1[l::p] = BA(1+(n-l)//p)
l += i*4+1
prime23[l::p] = BA(1+(n-l)//p)
if prime17[i]:
p = 30*i+17
l = i*(p+17)+9
prime19[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime23[l::p] = BA(1+(n-l)//p)
l += i*4+3
prime1[l::p] = BA(1+(n-l)//p)
l += i*6+3
prime13[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime17[l::p] = BA(1+(n-l)//p)
l += i*6+3
prime29[l::p] = BA(1+(n-l)//p)
l += i*4+3
prime7[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime11[l::p] = BA(1+(n-l)//p)
if prime19[i]:
p = 30*i+19
l = i*(p+19)+12
prime1[l::p] = BA(1+(n-l)//p)
l += i*4+2
prime17[l::p] = BA(1+(n-l)//p)
l += i*6+4
prime11[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime19[l::p] = BA(1+(n-l)//p)
l += i*6+4
prime13[l::p] = BA(1+(n-l)//p)
l += i*4+2
prime29[l::p] = BA(1+(n-l)//p)
l += i*2+2
prime7[l::p] = BA(1+(n-l)//p)
l += i*4+2
prime23[l::p] = BA(1+(n-l)//p)
if prime23[i]:
p = 30*i+23
l = i*(p+23)+17
prime19[l::p] = BA(1+(n-l)//p)
l += i*6+5
prime7[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime23[l::p] = BA(1+(n-l)//p)
l += i*6+5
prime11[l::p] = BA(1+(n-l)//p)
l += i*4+3
prime13[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime29[l::p] = BA(1+(n-l)//p)
l += i*4+4
prime1[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime17[l::p] = BA(1+(n-l)//p)
if prime29[i]:
p = 30*i+29
l = i*(p+29)+28
prime1[l::p] = BA(1+(n-l)//p)
l += i*2+1
prime29[l::p] = BA(1+(n-l)//p)
l += i*6+6
prime23[l::p] = BA(1+(n-l)//p)
l += i*4+4
prime19[l::p] = BA(1+(n-l)//p)
l += i*2+2
prime17[l::p] = BA(1+(n-l)//p)
l += i*4+4
prime13[l::p] = BA(1+(n-l)//p)
l += i*2+2
prime11[l::p] = BA(1+(n-l)//p)
l += i*4+4
prime7[l::p] = BA(1+(n-l)//p)
i+=1
except:
pass
# generation
RES = [2, 3, 5]
A = RES.append
ti=0
try:
for i in range(n):
if prime1[i]:
A(ti+1)
if prime7[i]:
A(ti+7)
if prime11[i]:
A(ti+11)
if prime13[i]:
A(ti+13)
if prime17[i]:
A(ti+17)
if prime19[i]:
A(ti+19)
if prime23[i]:
A(ti+23)
if prime29[i]:
A(ti+29)
ti+=30
except:
pass
if prime1[n] and (30*n+1)<=lim:
A(30*n+1)
if prime7[n] and (30*n+7)<=lim:
A(30*n+7)
if prime11[n] and (30*n+11)<=lim:
A(30*n+11)
if prime13[n] and (30*n+13)<=lim:
A(30*n+13)
if prime17[n] and (30*n+17)<=lim:
A(30*n+17)
if prime19[n] and (30*n+19)<=lim:
A(30*n+19)
if prime23[n] and (30*n+23)<=lim:
A(30*n+23)
if prime29[n] and (30*n+29)<=lim:
A(30*n+29)
return RES
def to_file(string, name):
f = file(name, "w")
f.write(string)
f.close()
def peremnozh(listt):
if len(listt) == 1:
return listt[0]
s = mpz(1)
for i in listt:
s *= i
return s
def perL1(listt, tread_n=-1, return_dict=None):
kol_v_gruppe = 15
countONgrup = 2
index = 0
while True:
index += 1
if kol_v_gruppe >= index:
countONgrup = kol_v_gruppe / index
if countONgrup == 1:
countONgrup = 2
kol_group = len(listt) / countONgrup
if kol_group == 0:
kol_group = 1
dlinna_grupi = len(listt) / kol_group
n_listt = []
while len(listt) != 0:
n_listt.append(peremnozh(listt[-dlinna_grupi:]))
del listt[-dlinna_grupi:]
listt = n_listt
if len(listt) == 1:
if tread_n == -1 and return_dict == None:
print "Zakonchili umnozhati"
return listt[0]
return_dict[tread_n] = listt[0]
break;
def perL2(listt, thread_count = 1):
if thread_count == 1:
return perL1(listt)
manager = multiprocessing.Manager()
return_dict = manager.dict()
countONgrup = 2
index = 0
while True:
if thread_count >= len(listt) / 2:
thread_count = len(listt) / 2
thread_list = []
dlinna_grupi = len(listt) / thread_count
index = 0
while len(listt) != 0:
proces = multiprocessing.Process(target=perL1, args=(listt[-dlinna_grupi:], index, return_dict))
index += 1
thread_list.append(proces)
proces.start()
del listt[-dlinna_grupi:]
for proc in thread_list:
proc.join()
n_listt = []
for i in xrange(thread_count):
n_listt.append(return_dict.values()[i])
listt = n_listt
if len(listt) == 1:
print "Zakonchili umnozhati"
return listt[0]
def kagda_zakonchiti(prostie, potolok):
a = 0
for i in prostie:
if i*i > potolok:
return prostie[a]
a +=1
def glav(dokuda, tread = 1):
print "shitaem primes"
prostie = naiti_prostie_menshe(dokuda)
print "zakonchili shitati primes"
print "dobavlyaem list"
ae = kagda_zakonchiti(prostie,dokuda)
for j in prostie:
if ae == j:
break
k = 1
while True:
k *= j
if k*j <= dokuda:
prostie.append(j)
else:
break
print "zakinchili dobavlyati list"
print "Umnozhaem"
return perL2(prostie, tread)
n = time.time()
st = str(glav(2000000000, 3))
to_file(st, "110.txt")
print time.time() - n