The prime 41, can be written as the sum of six consecutive primes:

41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

Which prime, below one-million, can be written as the sum of the most consecutive primes?


In [1]:
def primes_less_than_n(n):
    """ Returns  a list of primes < n """
    sieve = [True] * n
    for i in xrange(3,int(n**0.5)+1,2):
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
    return [2] + [i for i in xrange(3,n,2) if sieve[i]]

max_num_digits = 6
primes = primes_less_than_n((10**max_num_digits+1)-1)
print len(primes)

s_primes = set(primes)
def is_prime(p):
    return p in s_primes


78498

In [2]:
M = 10**max_num_digits
for num_terms in range(1,550):
    for s in range(len(primes)-num_terms):
        sm = sum(primes[s:s+num_terms])
        
        if is_prime(sm):
            print num_terms, sm
            break
            
        if sm > M:
            break


1 2
2 5
3 23
4 17
5 53
6 41
7 197
9 127
11 233
12 197
13 691
14 281
15 379
17 499
19 857
21 953
23 1151
25 1259
27 1583
29 2099
31 2399
33 2417
35 2579
37 2909
39 3803
41 3821
43 4217
45 4651
47 5107
49 5813
51 6829
53 6079
55 6599
57 14153
59 10091
60 7699
61 8273
63 10163
64 8893
65 9521
67 12281
69 13043
71 11597
73 12713
75 13099
77 16763
79 15527
81 16823
83 22741
85 21407
87 29023
89 26041
91 23011
93 22531
95 22037
96 22039
97 25759
99 28027
100 24133
101 37447
102 25237
103 28087
105 28099
107 36607
108 28697
109 36067
111 31729
113 35527
114 32353
115 33623
117 34913
119 41641
121 42391
122 37561
123 39607
124 38921
125 41017
127 42463
129 54617
130 43201
131 46181
132 44683
133 47711
135 54869
137 49279
139 65033
141 52517
143 54167
145 75253
146 55837
147 73571
149 67271
151 71011
152 61027
153 70141
155 64613
157 71167
158 66463
159 71191
161 72169
162 70241
163 76099
165 78121
167 78139
169 81203
171 84313
173 96001
175 101561
177 87511
178 86453
179 95107
181 108707
183 92951
185 110083
187 113719
189 105499
191 101999
192 102001
193 110323
195 113989
197 110359
198 109147
199 117751
201 115279
203 116531
204 116533
205 121621
206 119069
207 126781
208 121631
209 132001
211 137387
213 132049
214 129419
215 149993
216 132059
217 137477
219 138863
221 141671
223 151667
225 148817
227 170767
229 157561
231 157579
233 164999
235 172597
237 166541
239 171131
241 172687
243 207511
245 197969
247 182099
249 182107
251 198197
253 206489
255 214943
257 201599
259 203279
261 204979
263 230827
265 223919
267 213533
269 218749
271 231131
273 222269
275 225829
277 242171
279 240353
281 240371
283 240379
285 251609
287 263071
289 296479
291 255443
293 271003
295 265117
296 263171
297 269069
299 281023
301 289099
303 285091
305 283079
307 289171
308 287137
309 303643
311 305759
313 303691
315 314243
317 310019
319 340237
321 318557
323 325009
325 327193
326 325019
327 331603
328 329401
329 338269
330 333821
331 347251
332 338279
333 370033
334 342761
335 351811
337 354097
339 356387
341 360977
342 360979
343 370261
345 372607
347 382021
349 408251
350 379667
351 403511
353 403553
355 408431
356 393961
357 408461
358 398771
359 408479
361 423257
363 423287
365 425819
367 433421
369 433439
371 456587
373 441101
375 448859
377 448867
379 480533
381 526087
383 510073
385 507497
387 478001
389 483377
391 496877
393 499607
395 505067
397 516053
399 510569
401 527207
403 530021
405 541283
407 538487
409 535669
411 541339
413 558491
415 552751
417 573031
419 578923
421 576001
423 578959
425 584879
426 581921
427 590819
429 605849
431 605867
433 627217
435 611953
437 636553
439 624209
441 633487
443 668179
445 646027
446 642869
447 665089
449 655547
451 674749
453 674767
455 684493
457 730487
458 681257
459 697579
460 687767
461 697591
463 727409
464 700897
465 720791
467 734171
469 727499
471 778391
473 764783
475 747743
477 778643
479 792593
480 754573
481 803141
483 768371
484 768373
485 796259
487 817403
488 782263
489 796307
491 831697
493 821081
495 810457
497 817561
499 831847
501 846259
503 857137
505 857161
507 868123
509 860819
511 875491
512 868151
513 908849
515 961663
517 905189
519 905207
521 943003
523 954517
525 920291
527 946969
529 943153
530 935507
531 954697
533 966307
535 978001
536 958577
537 970219
539 978037
543 997651

In [ ]: