regular expression for primes

by Scott Saad - July 5, 2010

regular expression for primes

A regular expression for testing primes? Does this really work? At first look, it’s super ugly but then the beauty of it starts to seep out once one realizes that it really works!

And the magic formula is…

/^1?$|^(11+?)\1+$/

Discussion, links, and tweets

I'm a software geek living and working in Boulder, Colorado. Follow me on Twitter; you'll dig my tweets if you're into that sort of thing.