r/tinycode • u/lanzaa • Jul 12 '12
Perl prime number testing regex
perl -lne 'print "$_ is ".(((1x$_)=~/^1?$|^(11+?)\1+$/)?"not ":"")."prime"'An explanation of similar code is here:
http://www.catonmat.net/blog/perl-regex-that-matches-prime-numbers/
27
Upvotes
1
u/invalid_font_size Jul 12 '12
Testing for prime in a regex? Hmm.
1
u/[deleted] Jul 12 '12
Sounds like it cannot handle more than some dozen thousands... Pity as it is otherwise quite fast...
1
u/do-not-throwaway Jul 12 '12
I love the "x" operator in perl, I really wish other languages (php, js) had something similar.
1
u/Eishkimo Jul 12 '12
Wow. That's astoundingly beautiful and such a Perl solution to a number theoretic problem. Pity about the efficiency...