http://www.perlmonks.org?node_id=20724


in reply to RE: RE: RE: sieve of erothenes
in thread sieve of erothenes

Hadn't even thought about benchmarking them...

I suspect that it isn't my looping structure, but that I'm stepping across the primes low to high and you're going high to low. My code tests division by '2' first, so the inner loop only runs once half of the time. Thus only runs twice a third of the time, 4 times a fifth of the time, etc.

since you test the large numbers first, you have to make more tests to throw out a potential prime.

consider your test of 16:
16%13 -> 16%11 -> 16%7 -> 16%5 -> 16%3 -> 16%2 (not prime)
as opposed to mine:
16%2 (not prime)

The foreach may have temp array overhead, but it looks like I'm making up for it on number of % and iterations

/\/\averick