-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem50.py
51 lines (45 loc) · 1.01 KB
/
problem50.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
46
47
48
49
50
51
def is_prime(n):
"""Judge whether a number is prime
>>> is_prime(2)
>>> True
"""
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
def solution(n: int):
"""_summary_
>>> solution(100)
>>> 41
>>> solution(1000)
>>> 953
Args:
n (int): _description_
"""
i = 2
value = 0
ans = []
total = 0
while True:
if is_prime(i):
total += i
if total > n:
break
ans.append(total)
i += 1
ans = [0] + ans
# print(ans)
for i in range(len(ans)):
for j in range(i + 1, len(ans)):
if is_prime(ans[j] - ans[i]):
# if j - i == 543:
# print(j, i, ans[j] - ans[i])
# count = max(count, j - i)
value = max(value, ans[j] - ans[i])
return value
if __name__ == "__main__":
print(solution(100))
print(solution(1_000))
print(solution(1_000_000))