
A regular expression to check for prime numbers - kqr2
http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/
======
philh
Note: this is not a regular expression in the technical sense. It's common to
talk about "Perl-compatible regular expressions" (PCREs) or regexes to avoid
confusion.

No regular expression can check for prime numbers because the language { 1^n |
n is prime } is not regular. You can see this by applying the pumping lemma:
[http://en.wikipedia.org/wiki/Pumping_lemma_for_regular_langu...](http://en.wikipedia.org/wiki/Pumping_lemma_for_regular_languages)

~~~
_delirium
My theory-of-computation prof a few years ago was of the opinion that CS-
theory people should stop making that argument, and go with the standard usage
of "regular expression" being a particular syntax for writing matching
statements, of which PCRE syntax is the most popular, and use "regular
language" if they want the CS-theory meaning.

~~~
baddox
The problem with that is that it ruins the excitement from hearing that "a
regular expression can solve problem X." I believe most regex implementations
are actually Turing complete, so it's no surprise that a problem can be solved
with them. But that's just another CS-theory person argument.

~~~
_delirium
Yeah, I see regexes as more of a question of expression, in the pragmatic
human programmer sense, rather than formal expressibility. Some things are
natural and easy to write using regexes, and some are much better done some
other way, but I'm not sure the Chomsky hierarchy shows where that boundary
is.

~~~
baddox
I agree, however I feel that in almost every case, regexes are only natural
for matching regular languages.

------
silvajoao
The pattern detects if a number N is not prime by searching for A and B such
that N = A*B. The (11+?) searches for A bigger than 1, and the repetition of
this group (the \1+ part) repeats A as much as possible, eventually B times.
If an exact match isn't possible, it backtracks and advances A. If no such A
and B exist, A will keep increasing until it becomes bigger than N, and then
the match fails.

An interesting pattern that wasn't immediately obvious, but a very inefficient
way to detect primes :)

------
gojomo
This regex has been mentioned on HN a few times before.

If your browser has Java applet support, here you can watch the regex succeed
(demonstrating a number is composite) or fail (prime):

49:
[http://regex.powertoy.org/?pat=/^1%3F%24|^%2811+%3F%29\1+%24...](http://regex.powertoy.org/?pat=/^1%3F%24|^%2811+%3F%29\\1+%24/&syn=perlSyntax&anim=1&rep=&in=1111111111111111111111111111111111111111111111111)

47:
[http://regex.powertoy.org/?pat=/^1%3F%24|^%2811+%3F%29\1+%24...](http://regex.powertoy.org/?pat=/^1%3F%24|^%2811+%3F%29\\1+%24/&syn=perlSyntax&anim=1&rep=&in=11111111111111111111111111111111111111111111111)

------
meastham
If I recall correctly, Hofstatder uses the same principal for developing a
formal language for classifying primes in Goedel Escher Bach.

------
greenlblue
This has been around for a while but it's a really nice way to show what's
possible if you have backtracking capabilities.

------
ErrantX
The comments make for fun reading :)

My favourite:

 _for calculations with big numbers, try F#_

------
jlcgull
Fascinating!!

Yet scary how many folks confuse odd numbers with primes on the original
blog's comments.

