-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem50.py
More file actions
43 lines (35 loc) · 1.11 KB
/
problem50.py
File metadata and controls
43 lines (35 loc) · 1.11 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
"""
Consecutive Prime Sum
Project Euler Problem #50
by Muaz Siddiqui
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 euler_helpers import timeit, primes_to, is_prime
@timeit
def answer(n):
length = 0
longest = 0
primes = primes_to(n)
# Make a list of sums
prime_sum = [0]
for x in range(len(primes)):
prime_sum.append(prime_sum[x] + primes[x])
for a in range(len(prime_sum)):
b = a - (length + 1)
while b >= 0:
dif = prime_sum[a] - prime_sum[b]
if dif > n:
b -=1
break
if is_prime(dif):
length = a - b
longest = dif
b -=1
print("The prime is: " + str(longest) + " and consists of " + str(length) + " primes.")