I'm trying to build a sieve for project Euler solutions. I need primes up until about 100M, preferably with option to go much higher.
This implementation I have works fine, but is very slow:
class Primes:
__size = None
__sieve = []
__primes = []
def __init__(self, size):
self.__size = size
self.__sieve = [True] * size
for x in range(2, size):
if self.__sieve[x]:
self.foundPrime(x);
def foundPrime(self, x):
self.__primes.append(x)
for duplicate in range(2 * x, self.__size, x):
self.__sieve[duplicate] = False
For a sieve sized 100M, this initialization takes about 70 seconds on my fairly high-end computer. Does anyone know why? Because in Java and C# this took me about 1 second...
So, this post is different from other posts in that I do not want to know how to implement the algorithm, I want to understand why it is so slow in Python.
Some prints give me the information that about 50% of the time is spent on finding the first 100K primes.