-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp050.py
More file actions
executable file
·53 lines (43 loc) · 1.24 KB
/
p050.py
File metadata and controls
executable file
·53 lines (43 loc) · 1.24 KB
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
#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?
import logging
from prime import PrimeNumberPool
def ConsecutivePrime(n, prime, ml):
p = []
i = 0
sp = 0
while (i < len(prime.numbers) and prime.numbers[i] <= n):
while (sp < n):
p.append(prime.numbers[i])
sp += prime.numbers[i]
i += 1
if (sp == n):
return p
else:
if (len(p) < ml):
return [n]
sp -= p[0]
p = p[1:]
return p
def main(args):
ml = 1
mp = 2
if args.test:
m = 1000
else:
m = 1000000
prime = PrimeNumberPool(m)
logging.debug(len(prime.numbers))
for i in range(len(prime.numbers)):
p = prime.numbers[-i-1]
cp = ConsecutivePrime(p, prime, ml)
if (len(cp) > ml):
ml = len(cp)
mp = p
logging.info((mp, ml))