Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

RE: sieve of erothenes

by Adam (Vicar)
on Jul 01, 2000 at 03:05 UTC ( #20691=note: print w/ replies, xml ) Need Help??


in reply to sieve of erothenes

I tried to do it with less, but I could only beat you by a char or two based on spacing.

@_=(1); L:for(2..100) { $i=@_; while(--$i){next L unless $_ % $_[$i] } push @_,$_ } print join(" ",@_),"\n";
##########
My spacing:
sub Adam () { @_=(1);L:for(2..100){$i=@_;while(--$i){next L unless $_%$_[$i]}push +@_,$_} @_ } sub Maverick () { for(1..100){push @_,$_;for(@_[1..($#_-1)]){pop @_ and last unless $_ +[-1]%$_}} @_ }


Comment on RE: sieve of erothenes
Select or Download Code
RE: RE: sieve of erothenes
by maverick (Curate) on Jul 01, 2000 at 03:24 UTC
    I removed and extra set of parens, changed the unless to a if! and compressed out the extra whitespace and got us dead even.
    sub A { @_=(1);L:for(2..100){$i=@_;while(--$i){next L if!($_%$_[$i])}p +ush@_,$_} } sub M { for(1..100){push@_,$_;for(@_[1..$#_-1]){pop@_ and last if!($_[ +-1]%$_)}} } print join(" ",@_),"\n";
    I kept trying to think of how do this with maps and maybe come out with something different, but nothing has come to me yet

    /\/\averick

    ##Update##
    just saw how to squish out a 3 more characters
    sub A { @_=(1);L:for(2..100){$i=@_;while(--$i){next L if!($_%$_[$i])}p +ush@_,$_} } sub M { for(1..100){push@_,$_;for(@_[1..$#_-1]){pop@_&&last if!($_[-1] +%$_)}} }
      You win on the efficiency test too. with n = 100 we are roughly the same time, but as n gets large I get bogged down somewhere. I'm running a test with n = 1000000 on my dual proc, which has been hung for about five minutes now. But based on the out put I've already seen for smaller n's, I suspect that you will slaughter me time wise.

      I don't understand why yours is faster because you use two for loops and I use a while loop the second time, dropping the overhead of making the temp array that foreach generates. Now I'm curious about the efficiencies of the two looping mechanisms...

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://20691]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-12-26 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (173 votes), past polls