Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Well, looking through tilly's previous golf entries I think I'll feel fine if length($my_code) <= 3*length($tilly_code).

sub c{($k,@_)=($#_)?@_:2..($n=@_[0]);@_=map{($_%$k)?$_:0}@_;push @_,$k;$k<=sqrt($n)?c(grep{!/^0/}@_):@_}
As for the lenght:
[arguile@cobalt ~]$ wc -L sieve_golf 104 sieve_golf

True to the seive, it starts at k=2 eleminates n*k, then steps to next prime and repeats until k >= sqrt(n). Instead of mapping to 0 I would have loved to just drop them -- not bothering w/ the regexp for cleanup -- but I couldn't for the life of me figure out how.

The subroutine is recursive mainly b/c I had yet to try that technique and this seemed like a good time to try.

Sample output:

# display procedure blatantly stolen from Tye 2: 2 10: 2 3 5 7 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 +89 97 169: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 +89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167

Thanks to Petruchio for his rewording of ($k,@_)=($#_)?@_:2..($n=@_[0]) so I didn't produce an error. So far this seems like a great place to learn programming.</fawning>


Scratched recursion and went w/ a conventional for loop, brought it down to 87 chars w/ strict compliance.

# non-strict sub c{$n=pop;@_=2..$n;for($k=2;$k<=sqrt($n);($k)=@_){@_=grep!/^0/,(map +{($_%$k)?$_:0}@_),$k}@_} # strict w/ less named vars sub c{pop;@_=2..$_;for(my$k=2;$k<=sqrt;($k)=@_){@_=grep!/^0/,(map{$_%$ +k?$_:0}@_),$k}@_}


No one else was mapping, and now I see why (75 chars).
sub e{pop;@_=2..$_;for(my$k=2;$k<=sqrt;($k)=@_){@_=((grep$_%$k,@_),$k) +;}@_}

In reply to Re: (Golf): Sieve of Eratosthenes by Arguile
in thread (Golf): Sieve of Eratosthenes by tilly

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [ambrus]: Today I accidentally cut my hand while carrying a computer. On the display part of the motherboard that sticks out at the back of the chasis and has ports, there's this thin metal sheet with holes cut for the ports, to guide plugs into the sockets.
    [ambrus]: This sheet has sharp needle-like parts, 0.004 long and less than 0.001 wide, that can get bent to point outwards, and one of these cut into my palm when I lifted the box.
    [ambrus]: So now when I choose what motherboard to buy for my new home computer, I have one more specific property to guide me. Useful, because there's so many different boards to choose from.

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2017-01-16 19:32 GMT
    Find Nodes?
      Voting Booth?
      Do you watch meteor showers?

      Results (151 votes). Check out past polls.