-
Notifications
You must be signed in to change notification settings - Fork 0
/
pe50.py
59 lines (47 loc) · 1.43 KB
/
pe50.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
55
56
57
58
59
"""----------------------------------------------------------------------------
Project Euler
Gregory Gundersen
2013-02-07
Problem:
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 gmath
def get_primes(limit):
gen = gmath.gen_sieve_of_eratosthenes()
primes = []
while True:
p = gen.next()
if p <= limit:
primes.append(p)
else:
break
return primes
def get_sums(limit):
primes = get_primes(limit)
sums = [0]
sm = 0
for i in range(len(primes)):
sm += primes[i]
if sm <= limit: # this is the key to runtime success
sums.append(sm)
return sums
def main():
limit = 1000000
sums = get_sums(limit)
t = 1 # num of terms
result = 1 # max prime
for i in range(len(sums)):
for j in range(len(sums)):
n = sums[j] - sums[i] # sum
l = j-i # len
if gmath.is_prime(n) and l > t and n <= limit:
t = l
result = n
return result