Improve Project Euler Problem 10 Sol-1 (#5773)

* Improve Project Euler Problem 10 Sol-1

* Name correction

* psf/black formatting

* More formatting
This commit is contained in:
Rohan R Bharadwaj 2021-11-06 01:13:52 +05:30 committed by GitHub
parent 6b2b476f86
commit 48960268a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,11 +28,11 @@ def is_prime(n: int) -> bool:
True
"""
for i in range(2, int(sqrt(n)) + 1):
if n % i == 0:
return False
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))
def solution(n: int = 2000000) -> int:
@ -49,16 +49,7 @@ def solution(n: int = 2000000) -> int:
10
"""
if n > 2:
sum_of_primes = 2
else:
return 0
for i in range(3, n, 2):
if is_prime(i):
sum_of_primes += i
return sum_of_primes
return sum(num for num in range(3, n, 2) if is_prime(num)) + 2 if n > 2 else 0
if __name__ == "__main__":