-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem10.py
49 lines (38 loc) · 949 Bytes
/
problem10.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
import math
def solution(n=2000000):
"""_summary_
Args:
n (int, optional): _description_. Defaults to 2000000.
Returns:
_type_: _description_
"""
i = 3
ans = 0
while i < n:
if isprime(i):
ans += i
i += 1
return ans
def isprime(n):
"""_summary_
Args:
n (_type_): _description_
Returns:
_type_: _description_
"""
if 1 < n < 4:
# 2 and 3 are primes
return True
elif n < 2 or n % 2 == 0 or n % 3 == 0:
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
return False
# All primes number are in format of 6k +/- 1
for i in range(5, int(math.sqrt(n) + 1), 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
if __name__ == "__main__":
# print(isprime(4))
# print(solution(10))
# print(solution(10))
print(solution())