2 ms·
Add some spaces: sub sieve { sub p { $_[0] , @_>1 ? p(grep$_%$_[0],@_) : 1; } p(2..pop) } And if you get rid of som
by jrockway 16y ago
Add some spaces:
sub sieve {
sub p {
$_[0] , @_>1 ? p(grep$_%$_[0],@_) : 1;
}
p(2..pop)
}
And if you get rid of some terse syntax:
sub p {
my ($first, @rest) = @_;
if (@rest > 1){
return $first, p(grep { $_ % $first } @rest);
}
return 1;
}
sub sieve {
my $arg = shift;
return p(2..$arg)
}
BTW, you can even have Perl do this automatically for you, by running:
$ perl -MO=Deparse
<paste in the code>
sub p {
$_[0], @_ > 1 ? p(grep(($_ % $_[0]), @_)) : 1;
}
sub sieve {
p 2 .. pop @_;
}
But honestly, the first program is not incomprehensible if you've ever
programmed in Perl. Sure, it ain't literate programming, but it also
isn't beyond understanding if you have three minutes to spend on it.
It's an amusement, not your payroll system, after all.
- StavrosK 16y agoThanks, I have indeed never programmed in perl and was feeling a bit facetious.