Skip to content

Instantly share code, notes, and snippets.

Created Jun 23, 2018
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:
return primes
digits = primeSieve(2000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment