-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path050_consecutive_prime_sum.py
45 lines (32 loc) · 1.08 KB
/
050_consecutive_prime_sum.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
"""
idea:
check iterative for a certain chain length
"""
import time
import primesieve
def main():
primes = primesieve.primes(999999)
print("nums to test:", len(primes))
max_chain_length = -1
prime_with_max_chain_length = -1
for i in range(2, len(primes)):
prime = get_prime_with_given_chain_length(i, primes)
if prime != -1:
max_chain_length = i
prime_with_max_chain_length = prime
print("prime with chain length:", i, prime_with_max_chain_length)
print("max chain len", max_chain_length, "prime", prime_with_max_chain_length)
def get_prime_with_given_chain_length(length, primes):
prime_set = set(primes)
prime_sum = sum(primes[x] for x in range(0, length))
if prime_sum in prime_set:
return prime_sum
for i in range(length, len(primes)):
prime_sum = prime_sum - primes[i-length] + primes[i]
if prime_sum in prime_set:
return prime_sum
return -1
if __name__ == '__main__':
start_time = time.time()
main()
print("time:", time.time() - start_time)