Perl Prime Number Test

There is this amazing (yet slow) Perl regular expression that takes the brute force approach to finding whether a number is a prime or not. It is a really interesting use of regular expressions.

perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)1+$/' [number]

This one liner attempts to take your input number, n, and divide it by all numbers x, where x < 2 and x < n. For a full explanation, try the site where I first saw it.

  • Reddit
  • del.icio.us
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Digg

Leave a Reply