Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created August 16, 2022 22:33
Show Gist options
  • Save rishi93/11f267cfdf80b74ea6e6835a7ffe886a to your computer and use it in GitHub Desktop.
Save rishi93/11f267cfdf80b74ea6e6835a7ffe886a to your computer and use it in GitHub Desktop.
Solution to Project Euler problem #10
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