Created
August 16, 2022 22:33
-
-
Save rishi93/11f267cfdf80b74ea6e6835a7ffe886a to your computer and use it in GitHub Desktop.
Solution to Project Euler problem #10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
def eratosthenes(n): | |
primes = [] | |
sieve = [True for _ in range(n+1)] | |
p = 2 | |
while p**2 <= n: | |
m = p**2 | |
while m <= n: | |
sieve[m] = False | |
m += p | |
p += 1 | |
while not sieve[p]: | |
p += 1 | |
for i in range(2, n+1): | |
if sieve[i]: | |
primes.append(i) | |
return primes | |
if __name__ == "__main__": | |
primes = eratosthenes(2_000_000) | |
sum_primes = reduce(lambda x, y: x + y, primes) | |
print(sum_primes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment