Solution to problem 50

This commit is contained in:
David Doblas Jiménez 2021-09-20 17:29:22 +02:00
parent c92a9f1178
commit bcabf48bc3

51
src/Python/Problem050.py Normal file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env python3
"""
Created on 18 Sep 2021
@author: David Doblas Jiménez
@email: daviddoji@pm.me
Solution for problem 50 of Project Euler
https://projecteuler.net/problem=50
"""
from utils import timeit, list_primes, is_prime
@timeit("Problem 50")
def compute():
"""
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?
"""
ans = 0
result = 0
prime_list = list_primes(1_000_000)
for i in range(len(prime_list)):
sum = 0
count = 0
for j in prime_list[i:]:
sum += j
count += 1
if is_prime(sum) and count > result:
result = count
ans = sum
# print(sum, result)
if sum > 1_000_000:
break
return ans
if __name__ == "__main__":
print(f"Result for Problem 50: {compute()}")