0

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

Will Ness
  • 62,652
  • 8
  • 86
  • 167
tjjensma
  • 9
  • 2

1 Answers1

1

It looks like you are using Wheel Factorization with a 6-wheel, advancing by 2 and 4 alternately, taking advantage of the fact that all primes >= 5 are of the form 6n±1. This is faster than simple trial division, but not as fast as the Sieve of Eratosthenes. You are still doing some trial divisions by the prime numbers found in your list. The Sieve does not use division at all, it just uses addition which runs a lot faster.

rossum
  • 14,325
  • 1
  • 19
  • 34
  • yes, i am using wheel factorization, but i am not dividing anything , i multiply for example a prime with every number in the list that is not marked as non prime. it more like a list where first i multiply 2 with every number and mark those as non prime, then take the next number that is not marked as non prime(3) and multiply it with every number that is not marked as non prime (3*1, 3*3, 3*5, 3*7) and mark the results as non prime, and then this for 5, 7 etc. this is faster than just multiplying a prime with every number after it, since that way you calculate like 3 times 30 is not a prime. – tjjensma Jul 15 '17 at 11:34
  • never mind, what i have here is a version of the sieve of erastothenes – tjjensma Jul 15 '17 at 11:46
  • @tjjensma no actually, if you multiply them like you've described in your first comment, it's not [the sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) but the "Euler's sieve" (discussed on that same Wikipedia page). it *seems* like it should be faster than Eratosthenes, but it isn't. Eratosthenes comes with a small price of redundant crossings-off, and it's a good price to pay for its simple - and thus fast - operations overall, compared to Euler's insignificant savings of avoiding the redundant crossings-off at a price of its complex, and slower, operations. – Will Ness Jul 16 '17 at 16:15
  • Well implemented, Eratosthenes does not have redundant crossings off: use the numbers you have found so far to determine the next prime and start the crossings-off from p^2 for each new p. The redundancies appear between 2p and p^2, so they can be avoided. For example, all multiples of 7 below 49 will already have been crossed-off with 2, 3, and 5. – rossum Jul 16 '17 at 17:34