-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp050.py
49 lines (39 loc) · 1.19 KB
/
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
"""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 math import sqrt
LIMIT = 10**6
def primesUntil(n):
composite = [False for _ in range(n)]
for m in range(2, int(sqrt(n))+1):
if not composite[m]:
for i in range(m**2, n, m):
composite[i] = True
return [i for (i, m) in enumerate(composite) if (not m)][2:]
primes = primesUntil(5000)
def prime(n):
if n < 2: return False
for m in range(2, int(sqrt(n))+1):
if n%m == 0: return False
else: return True
length = len(primes) - 1
ans = 0
while not ans:
start = 0
s = sum(primes[start:(start+length)])
while (start+length) < len(primes) and s < LIMIT:
if prime(s):
ans = s
break
start += 1
s = sum(primes[start:(start+length)])
else:
length -= 1
start = 0
print(ans)