Regular Expression to check for primes

perl -wle ‘print “Prime” if (1 x shift) !~ /^1?$|^(11+?)\1+$/’ [number]

This is a nice trick to check if a number is prime using regular expressions, I am going to try and see how this performs compared to regular methods and see if anything interesting comes up.

Posted via email from Sijin Joseph

  • http://joshuakehn.com Joshua Kehn

    That’s pretty neat! I’m not sure how useful it would be, but interesting nonetheless.