Here's an odd pattern I found for sieving out composite numbers to find primes. I wrote it myself, originally thinking I made an indexing error, but it turned out to work after removing squares.
import bitarray
def sieve(n):
"""bitarray sieve"""
b=bitarray.bitarray([1]*(n+1))
x=int(n**.5)+1
for k in range(2,x):
if b[k]:
f=k+1 #this line is indexing error
e=n//k +1
for g in range(f,e):
if b[g]:
b[g*k]=0
return sorted(list(to_numbs(b)-squares_set(b)))
def squares_set(ba):
"""this returns a set of all primes**(2n).
note 0**2==0, 1**2==1"""
return {i**2 for i in range(int(len(ba)**.5)+1) if ba[i]}
def to_numbs(b_ar):
"""converts the bit array to a set of numbers"""
return {i for i in range(len(b_ar)) if b_ar[i]}
if __name__=="__main__":
print(sieve(100))
I'm not entirely sure what causes this pattern to work so I figured it would be fun to post here.