Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

RE: RE: RE: RE: sieve of erothenes

by maverick (Curate)
on Jul 01, 2000 at 08:44 UTC ( #20724=note: print w/replies, xml ) Need Help??

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


Replies are listed 'Best First'.
RE: RE: RE: RE: RE: sieve of erothenes
by husker (Chaplain) on Jul 05, 2000 at 18:05 UTC
    Although what I propose may not be the "classic" sieve algorithm, in truth you only need to test up to the primes which are less than the square root of the target number. In other words, you need not divide by any primes greater than 4 when testing 16. Based on the associative property of multiplication, if you can show that no numbers <= sqrt(n) evenly divide n, then you have also shown that there are no numbers => sqrt(n) which evenly divide n. While this may add a few chars to your code, it reduces the compute time enormously when you get to large n's.

    Update: Actually, it doesn't look like ANY of us are implenting the true Sieve algorithm. The algorithm can be found here: It does in fact use the sqrt(n) modification, but doesn't do divide-testing ... instead it does "weeding out" of multiples. Same result, different technique.

    Update 2: my mistake ... Ase is indeed doing the correct Sieve algorithm by using his bit vector technique.
RE: RE: RE: RE: RE: sieve of erothenes
by Adam (Vicar) on Jul 01, 2000 at 22:12 UTC
    You know, you are right. I had thought I had done it in the right order, but I didn't... I screwed that up. I guess I would have to use:
    @_=(1);L:for(2..100){$i=0;while(@_>++$i){next L if!($_%$_[$i])}push@_, +$_}
    which uses 2 more chars. Sigh.

    UPDATE: I can do it with one less char then above, but still too many.:

    @_=(1);L:for(2..100){$i=-@_;while(++$i){next L if!($_%$_[$i])}push@_,$ +_}
    Yes, I lie awake at night thinking about this stuff. No, not really. But when I get started thinking about it, I have a hard time stopping. But I doubt I'm alone in that disease. :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://20724]
[robby_dobby]: page for yapceu is out of date by 2 years
[robby_dobby]: LanX: Sharm-el-Sheikh is in Egypt
[robby_dobby]: Thanks, choroba
[LanX]: yes and Egypt (like Turkey and Russia) belongs to two continents
[choroba]: Also Europe and Asia are geographically one continent, the division is political
[robby_dobby]: I always thought everything on the other side of Suez to be Africa. That's an entire continent
[LanX]: Sinai_Peninsula
[robby_dobby]: choroba: If you're talking of Eurasia, it's only true of the erstwhile Constantinople/ Ottoman empire
[LanX]: yes correct, but Sinai is not on the other side

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2017-04-24 16:02 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (442 votes). Check out past polls.