We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 6b2b476 commit 4896026Copy full SHA for 4896026
project_euler/problem_010/sol1.py
@@ -28,11 +28,11 @@ def is_prime(n: int) -> bool:
28
True
29
"""
30
31
- for i in range(2, int(sqrt(n)) + 1):
32
- if n % i == 0:
33
- return False
34
-
35
- return True
+ if 1 < n < 4:
+ return True
+ elif n < 2 or not n % 2:
+ return False
+ return not any(not n % i for i in range(3, int(sqrt(n) + 1), 2))
36
37
38
def solution(n: int = 2000000) -> int:
@@ -49,16 +49,7 @@ def solution(n: int = 2000000) -> int:
49
10
50
51
52
- if n > 2:
53
- sum_of_primes = 2
54
- else:
55
- return 0
56
57
- for i in range(3, n, 2):
58
- if is_prime(i):
59
- sum_of_primes += i
60
61
- return sum_of_primes
+ return sum(num for num in range(3, n, 2) if is_prime(num)) + 2 if n > 2 else 0
62
63
64
if __name__ == "__main__":
0 commit comments