-
Notifications
You must be signed in to change notification settings - Fork 0
/
p050.py
45 lines (31 loc) · 772 Bytes
/
p050.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
from math import sqrt
def is_prime(x):
for n in range(2, int(sqrt(x)) + 1):
if x % n == 0:
return False
return True
def primes(b):
yield 2
for n in range(3, b, 2):
if is_prime(n):
yield n
b = 1_000_000
p = list(primes(b))
m = 0
l = None
for i in range(0, len(p)):
for j in range(i + 1, len(p)):
s = sum(p[i:j])
# It is really just a trimmed brute-force search.
# These three exit conditions make the search possible...
if s >= b:
break
if j - i < m:
continue
if len(p) - i < m:
break
if s in p:
if len(p[i:j]) > m:
m = len(p[i:j])
l = p[i:j]
print(l, m, sum(l))