-
Notifications
You must be signed in to change notification settings - Fork 0
/
p050.py
51 lines (39 loc) · 895 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
46
47
48
49
import time
def sieve(n):
a = set(range(3, n + 1, 2))
a.add(2)
for i in range(3, int(n/2) + 1, 2):
if i in a:
a.difference_update(range(2 * i, n, i))
return a
def main(n):
start = time.time()
primes = sieve(n)
lst = sorted(primes)
top = 0
prime = 0
print(time.time() - start)
# starting with 2
check = 0
for i in range(1, len(lst), 2):
check += lst[i - 1]
check += lst[i]
if check in primes:
if (i + 1) > top:
top = (i + 1)
prime = check
if check > n:
break
for i in range(1, len(lst)):
check = lst[i]
for j in range(i + 1, len(lst) - 1, 2):
check += lst[j]
check += lst[j + 1]
if check in primes:
if (j + 2) - i > top:
top = (j + 2) - i
prime = check
if check > n:
break
print(time.time() - start)
return prime, top