in reply to Sieve of Eratosthenes Golf?

Here's 58 57 characters that even does all the printing, though it needs to be run with perl -l:

sub f{@_?($_[0],f(grep$_%$_[0],@_)):()}print for f 2..pop

I used a recursive filtering approach to make it much more concise. In fact, the sub that does the work is only 39 characters.

Update: I noticed an easy way to shave a stroke (grep EXPR vs grep BLOCK), so now it's at 57. I also added code tags which I stupidly omitted.

blokhead

Replies are listed 'Best First'.
Re^2: Sieve of Eratosthenes Golf?
by Cody Pendant (Prior) on Apr 07, 2005 at 04:16 UTC
    The guys above you seem to have proceeded on the assumption that they should write (the contents of) a sub which returned the correct numbers, whereas yours prints them and you've included the sub block too. Not sure what the rules of "golf" are at this point but you're definitely ahead!


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
    =~y~b-v~a-z~s; print
Re^2: Sieve of Eratosthenes Golf?
by dragonchild (Archbishop) on Apr 07, 2005 at 13:07 UTC
    That's a very interesting take on the Sieve. Rewritten to be golfed, it's 33 characters.
    @_?($_[0],f(grep$_%$_[0],@_)):()

    It needs to be called as f(2..$limit) vs. f($limit).

    If the requirement is that the signature is f($limit), then it goes to 47 characters
    sub x{@_?($_[0],x(grep$_%$_[0],@_)):()}x 2..pop