2019-07-18 03:29:59 +02:00
|
|
|
from lib_prime import primes
|
|
|
|
|
|
|
|
|
|
|
|
def find_max_series(start_index, series_list, series_set):
|
|
|
|
""" Start at the given index and sum up consecutive primes
|
|
|
|
into total. If a total is in the series_set (i.e. a prime) then
|
|
|
|
store the current number of consecutive primes (length) and the total.
|
|
|
|
If the total exceeds the last value of the given primes no longer
|
|
|
|
sequence can be found. Return the max sequence length and total. """
|
|
|
|
series_max = series_list[-1]
|
|
|
|
total_max = 0
|
|
|
|
total = 0
|
|
|
|
for i in range(start_index, len(series_list)):
|
|
|
|
total = total + series_list[i]
|
|
|
|
if total in series_set:
|
|
|
|
length = i - start_index + 1
|
|
|
|
total_max = total
|
|
|
|
if total > series_max:
|
|
|
|
return (length, total_max)
|
|
|
|
|
2019-07-17 03:17:21 +02:00
|
|
|
|
|
|
|
def euler_050():
|
2019-07-18 03:29:59 +02:00
|
|
|
n_max = 1000000
|
|
|
|
ps = primes(n_max)
|
|
|
|
ps_set = set(ps)
|
|
|
|
s = max([x for x in [find_max_series(i, ps, ps_set)
|
|
|
|
for i in range(0, len(ps))] if x])
|
|
|
|
return s[1]
|
2019-07-17 03:17:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print("e050.py: " + str(euler_050()))
|
2019-07-18 03:29:59 +02:00
|
|
|
assert(euler_050() == 997651)
|