Originally posted by laserlight
hmm... but mine is still slower than yours, which puzzles me.
At first I thought that was because my overhead of 3 iterations to first create an array of potential primes, then applying the sieve of Erasthosthenes, and then filtering the result took too much time in the given 3 seconds.
I increased the timeframe to 10 seconds, but your code was still faster.
I do think it's the initialisation overhead (which is why I tried to populate my arrays dynamically) - I haven't tried to find out just where, but I'm sure yours will pull out in front eventually; loop performance seems curiously slow in PHP for some reason. I suspect it's the whole "copy-on-write" thing of the Zend engine. I think everytime the loop index is incremented a new value is created somewhere in memory, the variable is pointed to it, and the old one is gc'd. But I haven't looked at the code at all to verify this.
Um, another legalistic niggle: "6/11/2004, midnight". That's 2004-11-06 00:00:00? Which timezone? Not that I care that much (I doubt I'll do any better than
function check_prime($num) {
if($num==2 || $num==3)
return true;
if($num==5 || $num==7 || $num==11 || $num==13)
return true;
if(abs($num%6-3)!=2)
return false;
if(!($num%5 && $num%7 && $num%11 && $num%13))
return false;
$sqrt = ceil(sqrt($num))+6;
for($i=18; $i<=$sqrt; $i+=6)
{
if(!($num%($i+1) && $num%($i-1)))
return false;
}
return true;
}
before then, but, hey), we're programmers here. We have to be pedantic 😃
ps: I think the above is my final entry.