views:

165

answers:

4

Hi All, I did some searching and was not able to find any information regarding this implementation versus every other one I have seen.

function sieve($top)
{
    for($i = 11; $i<$top; $i+=2)
    {
         if($i % 3 == 0 || $i % 5 == 0 
            ||  $i % 7 == 0)
          {
             continue;
          }
       echo "$i <br />";
   }
}

Yeah I know it just prints it out, but that's not the important part. What is the major pitfall whether it be time or other?

EDIT: Are there any other issues beyond scalability? Also again thanks for the comments about moving forward with prime finding.

+4  A: 

The major pitfall of this is it doesn't scale. Once the numbers are large enough anything will be returned. You list of modulus excluders needs to grow with the search.

ck
From what I have seen that is also the major pitfall for this algorithm in general. I hear 10 ^ 9 is the real breaking point for this algorithm in general.
Woot4Moo
your version fails at about 10^2 :)
Peter Recore
@Peter: actually, as 7 is the largest known prime he is *sieving* against, anything above 49 is not guaranteed. @Woot4Moo: is this something you are doing as practice, as you are quite a long way off reinventing the wheel if you need a full solution. I wrote myself a prime-finder jsut to test CPU operational speed and multithreading, and rather than use the SOE, I just did number crunching checking for any lack of remainder when dividing by 3 += 2...
ck
@ck it was just something to get used to the syntax of php. No real application in what I do on a daily basis.
Woot4Moo
A: 

It's limited to prime numbers up to 11. To extend it any further you need to add || $u % 11 == 0 || $i % 13 == 0 ... etc

eduffy
My thought is since anything divisible by 2,3,5, or 7 is not prime I would not need to do % 11 or % 13. Since with integer multiplication I do not have to worry. Are there any specific situations where I could multiply 2 numbers that would not make that statement evaluate to true?
Woot4Moo
I'm not sure what you mean, but I think a counter example is 121, which is 11*11 and is not divisible by 2, 3, 5, or 7. You need to check against each prime less than the square root of a number in order to determine whether it's prime.
StrixVaria
@Woot4Moo - you are right, anything that is divisible by 2, 3, 5 or 7 is not prime, but the reverse is not true. Greg's comment to your question has the example of 221 which is not divisible by any of these, but is also not prime.
ck
thank you, that is one of those things that I was hoping for some confirmation on.
Woot4Moo
A: 

You can refer to Sieve of Eratosthenes on Wikipedia; and this link for a PHP implementation.

Rubens Farias
A: 

First, you're only checking against three numbers (3, 7, and 11). For the Sieve of Erathosthenes, you should start with a list of numbers, 2..i. Then loop through that list, and remove numbers that are factors of the number you're iterating on. For example, once you get to 7, which is prime, you'll need to remove 49, 56, and other multiples of 7.

Second, the method I just described would scale very poorly - if you tried looking for primes from 1..10^9, you'd need 10^9 values in your list. There are other ways besides the Sieve of Erathosthenes to find prime numbers - see http://en.wikipedia.org/wiki/Prime%5Fnumber

David
If you were looking for primes from 1 to 10<sup>9</sup>, you'd need many numbers, but fewer than 10<sup>9</sup>... it would be in the tens of thousands, though.
Chip Uni
Good point. I should have added, "in this naive implementation, ..."
David