02 August 2010

Primality test

kottke.org pointed out this egregious abuse of regular expressions some time ago, but the page he referenced never made it into my post, and I can't find Kottke's post in his archives. In the course of trying to track down the page, I found several variations and amplifications of /^(11+)\1+$/ as a regular expression to find prime numbers in Perl, Python, or what have you.

See posts from Brontosaurus, Peteris Krumins, Neil Kandalgaonkar, Avinash Meetoo, and this Stack Overflow thread. There is a Perl one-liner by Abigail (last name?) from a 1997 Perl Journal that perhaps takes historical priority.

No comments: