Skip to content

Instantly share code, notes, and snippets.

@shoupn
Created Jun 23, 2018
Embed
What would you like to do?
Sieve of Eratosthenes Algorithm for Finding Prime Numbers
import math
def primeSieve(sieveSize):
# Returns a list of prime numbers calculated using
# the Sieve of Eratosthenes algorithm.
sieve = [True] * sieveSize
sieve[0] = False # zero and one are not prime numbers
sieve[1] = False
for i in range(2, int(math.sqrt(sieveSize)) + 1):
pointer = i * 2
while pointer < sieveSize:
sieve[pointer] = False
pointer += i
# compile the list of primes
primes = []
for i in range(sieveSize):
if sieve[i] == True:
primes.append(i)
return primes
digits = primeSieve(2000000)
print(sum(digits))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment