Lately I have done some reading about primes, and decided to make my own program for finding them (not for school or anything, just hobby). this is the code:
import time
a=[2,3]
e=[0,1,1,1,0,1]
b=0
c=0
d=[]
length=int(input("primes til n?"))
tijd=time.time()
while 1:
d=[]
b=(e.index(0,a[-1])+1)
a.append(b)
if len(e)<length:
e.extend(e*(b-1))
e[(b-1)]=1
if ((b**2)-1)>len(e):
break
d.append((b**2)-1)
c=b
while (((e.index(0,c)+1)*b))<len(e):
d.append(((e.index(0,c)+1)*b)-1)
c=(e.index(0,c+1))
for getal in d:
e[getal]=1
e.append(0)
while(e.index(0,b))<(len(e)-1):
b=((e.index(0,(b+1)))+1)
a.append(b)
print(len(a))
print(time.time()-tijd)
i know the code is not as readable as it can be, and i know improvements can be made.(but i do not have that much python experience)
i was wondering if anyone knows other python prime finding algorithms to compare its speed with. and id also like to know if this method of finding primes already exists, since i was not able to find something like this online.(this is just for hobby, not school btw)
explanation of the code:
variable a is a list of all founded primes variable e is a list whit ones and zeroes where [0,1,1,1,0,1] stands for:1 and 5 could be primes and 2,3,4 and 6 are not (yes i know 2 and 3 are primes, but they are already in the prime list)
so the first thing the program does is til which number you want the primes.(though it does not give the primes til that number, but the first higher product of primes.
then in the while loop it says b= the next prime is the index of the first zero in the list with ones and zeroes, starting with counting from 1, since the first zero does not count.
then if the length of the list with ones and zeroes is smaller than the requested length, multiply the list with the most recent prime( so if the list was [0,1,1,1,0,1] and the most recent prime is 5 (first zero starting counting after he first, it multiplys the list with 5, so it is [0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1]
then stop if the recent prime squared is larger then the list size
then what it does is take the recent prime(5) and multiply it with the index of al numbers that are zero in the list [0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1] so it would do 1*5,5*5 and replace the cells in the list with index 5 and 25 with an 1 so the new list will be [0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1]
when the program is done with this first part of the program all cells that are 0 in the list will be primes