views:

1852

answers:

3

I am doing a project at the moment and I need an efficient method for calculating prime numbers. I have used the sieve of Eratosthenes but, I have been searching around and have found that the sieve of Atkin is a more efficient method. I have found it difficult to find an explanation (that I have been able to understand!) of this method. How does it work? Example code (preferably in C or python) would be brilliant.

Edit: thanks for your help, the only thing that I still do not understand is what the x and y variables are referring to in the pseudo code. Could someone please shed some light on this for me?

A: 

The Wikipedia page looks pretty comprehensive - it even has links to implementations in C and Python.

Dave Rigby
+3  A: 

The wiki page is always a good place to start. If you see the end of the page, it links to examples in both C and Python:

Here's the Python function, copied from the second link:

def sieveOfErat(end):  
  if end < 2: return []  

  #The array doesn't need to include even numbers  
  lng = ((end/2)-1+end%2)  

  # Create array and assume all numbers in array are prime  
  sieve = [True]*(lng+1)  

  # In the following code, you're going to see some funky  
  # bit shifting and stuff, this is just transforming i and j  
  # so that they represent the proper elements in the array.  
  # The transforming is not optimal, and the number of  
  # operations involved can be reduced.  

  # Only go up to square root of the end  
  for i in range(int(sqrt(end)) >> 1):  

    # Skip numbers that aren’t marked as prime  
    if not sieve[i]: continue  

    # Unmark all multiples of i, starting at i**2  
    for j in range( (i*(i + 3) << 1) + 3, lng, (i << 1) + 3):  
      sieve[j] = False  

  # Don't forget 2!  
  primes = [2]  

  # Gather all the primes into a list, leaving out the composite numbers  
  primes.extend([(i << 1) + 3 for i in range(lng) if sieve[i]])  

  return primes
Noldorin
As an aside; what's the general policy here for citing other websites? The reason I ask is I considered copy-pasting one of the examples from the wikipedia article, but I decided against it as that's a perfectly good resource and I'd just be duplicating the content here; the better solution would to encourage the OP to look on wikipedia first for standard questions such as this. Any thoughts?
Dave Rigby
@Dave: I understand your slight confusion regarding this. The policy is generally to quote the web page from which you are taking the information (either text or code) and then paste as much as appears sensible from the appropiate parts of the page. There are two primary reasons for this: a) convenice to whomever is reading the answer, b) if the linked web page happens to go down/be modified at any point, the original content is still preserved on SO. Not all of these reasons apply in all cases, but it's nonetheless good practice. Crediting the source is of course a must, as you have done.
Noldorin
Saying that, encouraging someone do do their own research is usually a good thing. In this case, since we've already pointed the OP to the wiki page, it's not really doing to much to provide the code example too, IMO.
Noldorin
The wiki page was the first page I looked at in relation to the problem but I didn't fully understand the pseudo code. When I looked elsewhere a lot of people had copied and pasted the wiki page so I wasn't able to find the answers to my questions (that I could understand).
marc lincoln
At the time of this writing, krenzel.info is down. So I'm glad someone copied the Python code here - that's what I was looking for :)
jnylen
A: 

He creado mi propia Criba que no necesita cuadrados, por tanto no es parecida a la de erastotenes, pero con muchas diferencias, por tanto me permite sacar del 1 al 600 millones cuales son primos en 14 segundos, en una laptop n200 en lenguaje c y en php un lenguaje lento, 100 millones estoy buscando a que sociedad de matematica se le presentan resultados. Estoy a punto de cambiar el programa para que funcione en redes al cambiar la forma de almacenar los datos. Ivan Mrsnik

Translation (courtesy of Google Translate)

I created my own sieve need not square, so there is similar to that of Eratosthenes, but with many differences, so I can get 1 to 600 million of whom are cousins in 14 seconds in a laptop n200 c language in php slow language, 100 million'm looking to society mathematical results are presented. I'm about to change the program to run on networks by changing the way of storing data. Ivan Mrsnik

ivan