-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_050.py
54 lines (36 loc) · 1.22 KB
/
problem_050.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
# coding: utf-8
'''
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?
'''
from itertools import accumulate
from helpers import eratosthene
def result(_max):
list_primes = list(eratosthene(_max))
list_sum = list(accumulate(list_primes))
length = 0
for counter, i in enumerate(list_sum):
if i in list_primes and counter > length:
length = counter
result = i
for i in range(len(list_sum)):
for j in range(i - length - 1, 0, -1):
diff = list_sum[i] - list_sum[j]
if diff > _max:
break
if diff in list_primes:
length = i - j
result = diff
return result
def test_result():
assert result(100) == 41
assert result(1000) == 953
def main():
return result(1000000)
if __name__ == '__main__':
test_result()
print(main())
# 997651 in