4 ms·
Very interesting. Thanks for writing this up. This is a bit more Perlish (not that I'm an authority): sub is_prime { (1x$_[0]) !~ /^(?:.?|(.{2,}?)\1
by mwpmaybe 10y ago
Very interesting. Thanks for writing this up.
This is a bit more Perlish (not that I'm an authority):
sub is_prime {
(1x$_[0]) !~ /^(?:.?|(.{2,}?)\1+)$/;
}
But perhaps less clear to a novice reader. You can also leave off the semicolon.
- iluxonchik 10y agoThank you :) You are probably right, but this function was my first head-to-head encounter with Perl, I researched just a little bit what I needed to know in order to write it.
- bigiain 10y agoperl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/' [number] (My first exposure to that was in '97 or so as a .sig from abigail in comp.lang.perl.misc)
- peteretep 10y agoSome asshole gave me this in an interview for a Perl dev job, and asked me what it did.
- bigiain 10y agoWasn't at an interview, but I remember quizzing the "new guy" Perl dev with "So explain what this one does": @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, foo($_)] } @unsorted; (Twenty years or so back, I could occasionally be "that asshole"... I'm better now, honest...)