JOEZACK.COM Code Musings and Such

18Jan/090

Prime Number Regex!

This is hardly news to the internet but it's very much news to me, and very much awesome. I was doing some research on prime numbers for one of the Project Euler problems when I came across a regular expression that determines if a number is prime!

("1" * n) !~/^1?$|^(11+?)\1+$/

Here's an nice explanation of how it works. Neat.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)