3 ms·
Wow... and I was happy with: sub mystery{for($t=3;$t*$t<$_[0];$t+=2){if(!$c[$t]){for($s=$t*$t;$s<$_[0];$s+=$t*2) {$c[$s]++}}}$r[++$#r]='2';for($t=3;$t<
by elliottcarlson 16y ago
Wow... and I was happy with:
sub mystery{for($t=3;$t*$t<$_[0];$t+=2){if(!$c[$t]){for($s=$t*$t;$s<$_[0];$s+=$t*2)
{$c[$s]++}}}$r[++$#r]='2';for($t=3;$t<$_[0];$t+=2){$c[$t]||push(@r,$t)}return(@r)};
(Based on http://www.c2.com/cgi/wiki?SieveOfEratosthenesInManyProgrammingLanguages http://www.c2.com/cgi/wiki?SieveOfEratosthenesInManyProgramm... but with a fun push alternative and in function form)
- jrockway 16y agoAny time you see a three arg for loop in perl golf, it's probably Doing It Wrong.