I'm trying to write a function that can find all the prime numbers below some very large number by using the Sieve of Eratosthenes. I have written the function:
def primes(limit):
#efficient method for finding large primes
l=set()
i=1
while i<limit+1:
l.add(i)
i+=2
s=int(math.sqrt(limit))
#recur until sqrt is small
if s<=1000:
ps=smallprimes(s)
else:
ps=primes(s)
for p in ps:
l-=set(multiples(p,limit+1)[1:])
return [2]+(list(l)[1:])
where smallprimes calculates primes below a limit just by checking the number of factors, and multiples calculates all multiples of a number below a limit.
With very large limits passed to primes
, I create the large sets to "strike out" multiples of all the primes below the square root of limits
.
Is there a more efficient way to "strike out" numbers from a sequence than by using sets? I am wondering because I really only need to subtract two arrays, I don't need prevention of duplicates, etc.