-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp050.py
More file actions
60 lines (56 loc) · 1.44 KB
/
Copy pathp050.py
File metadata and controls
60 lines (56 loc) · 1.44 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from math import sqrt
import time
def primes_till(n):
p = [2, 3, 5, 7, 11]
for i in range(13, n):
prime = True
for num in p:
if num > sqrt(i):
break
if i%num == 0:
prime = False
break
if prime:
p.append(i)
return p
def sieve_eratosthenes(n):
is_prime = [True]*n
for i in range(2, n):
if is_prime[i]:
for j in range(2*i, n, i):
is_prime[j] = False
primes = [p for p in range(2, n) if is_prime[p]]
return primes
if __name__ == '__main__':
tStart = time.time()
# l = primes_till(1000000)
l = sieve_eratosthenes(1000000)
no_of_primes = len(l)
print(len(l))
# print(l)
res = 953
# find the maximum length sequence
tmp_sum = 0
con = 0
for p in l:
tmp_sum += p
con += 1
if tmp_sum > 1000000:
break
print('Maximum no of consecutive primes %d' % con)
found = False
while con > 21:
for s in range(no_of_primes - con + 1):
tmp = sum(l[s:s+con])
if tmp >= 1000000:
break
if sum(l[s:s+con]) in l:
found = True
res = sum(l[s:s+con])
print(res, con, s)
break
if found:
break
con = con - 1
tEnd = time.time()
print('time taken %f seconds' % (tEnd - tStart))