note
Arguile
<p>Well, looking through tilly's previous golf entries I think I'll feel fine if <code>length($my_code) <= 3*length($tilly_code)</code>.
<code>
sub c{($k,@_)=($#_)?@_:2..($n=@_[0]);@_=map{($_%$k)?$_:0}@_;push
@_,$k;$k<=sqrt($n)?c(grep{!/^0/}@_):@_}
</code>
As for the lenght:
<code>
[arguile@cobalt ~]$ wc -L sieve_golf
104 sieve_golf
</code>
<p>True to the seive, it starts at <code>k=2</code> eleminates <code>n*k</code>, then steps to next prime and repeats until <code>k >= sqrt(n)</code>. 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.</p>
<p>The subroutine is recursive mainly b/c I had yet to try that technique and this seemed like a good time to try.</p>
<p>Sample output:
<code>
# 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
</code>
<p>Thanks to [Petruchio] for his rewording of <code>($k,@_)=($#_)?@_:2..($n=@_[0])</code> so I didn't produce an error. So far this seems like a great place to learn programming.</fawning></p>
<br>
<h4>Update</h4>
<p>Scratched recursion and went w/ a conventional for loop, brought it down to 87 chars w/ [strict] compliance.</p>
<code>
# 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}@_}
</code>
<h4>Update</h4>
No one else was mapping, and now I see why (75 chars).
<code>
sub e{pop;@_=2..$_;for(my$k=2;$k<=sqrt;($k)=@_){@_=((grep$_%$k,@_),$k);}@_}
</code>
81695
81695