Even getting all the prime numbers less than 1000, the Sieve method is roughly 3 times faster than the method shown above. I would expect its advantage to grow as the number of prime numbers desired grows.

EDIT: I couldn't stand it; had to test my gut feelings.

Yes, if you ask for all prime numbers less than 5000, then the Sieve outperforms the modulo by a factor of *TEN*.

Not surprising, but I just wanted to check my sanity.

06-26-2013, 05:51 AM

Devin_p

Thank you for all your help and effort you put into this! :thumbsup:

06-26-2013, 05:58 PM

Old Pedant

Just for completeness, here's the version of the Seive of Erastothenes that I used:

There are ways to make that a little more efficient, but not enormously so.

06-26-2013, 06:30 PM

Airblader

Warning: Some math up ahead.

Quote:

Originally Posted by Old Pedant

But even better: Why try moduloing the candidate with *every* number (even every odd number)?? Why not just try testing it against ALL THE PRIMES LESS THAN itself!

Even better: Only try all integers less than or equal to the square root of the number [1][2] :) And now combining these two solutions, even better is to check all prime numbers less than or equal to the number [3].

As for algorithms in general: First off, the Sieve of Eratosthenes could be improved, but one could also use another optimized version known as the Sieve of Atkin which is better by a factor of 1/(log log n).

[1] If p was not prime, it could be factored into p = ab. Without loss of generality, assume a > sqrt(p). It would immediately follow that b < sqrt(p). Since finding only one factor suffices to disprove that p is a prime, it is enough to check all integers less than or equal to the square root of the number in question.

[2] The number of primes <= x is asymptotically equivalent to x/ln(x). The limit of that divided by sqrt(x) is -- as one can see with fairly basic maths -- infinite, which means that it is by far better to use sqrt(x). As an additional benefit the test is independent of all prime numbers smaller than the number in question.

[3] Again, if p was not prime, there would be a factorization p = ab. In [1] we have seen that we can assume a <= sqrt(p). Now, if a was not prime itself, it could be further factorized into a = xy. The same argument would show (without loss of generality) that x <= a <= sqrt(p).

06-26-2013, 06:38 PM

DrDOS

Quote:

Originally Posted by Old Pedant

But even better: Why try moduloing the candidate with *every* number (even every odd number)?? Why not just try testing it against ALL THE PRIMES LESS THAN itself!

Actually, it only needs to be test against all primes less than the square root of itself, so you just need to make a list of them.

06-26-2013, 07:52 PM

Old Pedant

Well, even the version of the Sieve of Erastothenes that I used is not the best, as I said. I know it can easily be made about 4 times as efficient. But that code is pretty simple and compact, and unless you are going for a huge number of primes it's more than adequate.

06-27-2013, 07:10 AM

Airblader

@DrDos
Less than or equal to(!) -- this is crucial, otherwise you would count every square of a prime number as prime.

@OldPedant

I agree that there is no need to improve it if it satisfies your needs. But switching to the square root is not a factor of four, we're talking orders of magnitude and a legit asymptotic benefit here. It's not a very special improvement either, it's fairly common to write even simple implementations using the square root as the bound. So yes, for the first few hundred primes it should be practically irrelevant, but I believe that with technical algorithms like this, at least showing what impact little details can have is important. :)

06-27-2013, 06:41 PM

Old Pedant

Okay, I give up. How does using the square root of the number being checked apply to the Sieve of Erastothenes algorithm???

Look at my code in post #19.

To do the prime.push(chk), we must loop all the way to the max num we are wanting to check.

And then the inner loop must also go all the way to that same max num to be sure all the multiples of the given prime are marked.

But notice that the marking loop at least only starts at the found prime number, so it's not terrible. But we could get 4 times as efficient by only considering odd numbers and by not bother with marking even multiples of a prime number. Oh, and we don't need to mark the prime number we just found.

Further than that...I dunno. What do you think can be done? And how does square root apply to this algorithm??