

A Perl Regular Expression That Matches Prime Numbers - pkrumins
http://www.catonmat.net/blog/perl-regex-that-matches-prime-numbers/

======
xyzzyz
It's cool, but it's regexp, not regular expression -- regular expressions
cannot test for primeness. The proof is a simple application of pumping
lemma[1]: we want to show that language { a^p: p prime } is not regular.
Suppose it is. By pumping lemma we have numbers N, n >= 1, such that for every
k >= 0, we have a^(N+nk) in our language. But that means that for every k,
N+nk is prime, and it fails for k = N, since N+nN = N(1+n) is not a prime,
since both N and 1+n are greater than 1.

[1] -
[http://en.wikipedia.org/wiki/Pumping_lemma_for_regular_langu...](http://en.wikipedia.org/wiki/Pumping_lemma_for_regular_languages)

~~~
lsb
That argument is theoretically sound, and I've upvoted you for bringing up
such a helpful contribution, but it's practically nonsense.

We are all running computers that are finite-state machines: if you have
roughly a trillion bits of storage, you can have up to 2^1T states.

For most numbers that are interesting to humans, we are able to test their
primality with regular languages, which are detectable by finite-state
machines. The number of states might be quite large indeed.

~~~
xyzzyz
You're mostly right, I made a similar point few months ago[1].

However, I must disagree that we are able to use regular expressions to test
for primality all the number we're interested in. Your argument only proves
that there _exists_ a regular expression that tests for primality all the
numbers below N, where N is arbitratily large. It does not shows any way how
to construct it[2], nor does it guarantee that the DFA we will be using to
test for primality will fit in available memory.

[1] - <http://news.ycombinator.com/item?id=3046623>

[2] - It's actually trivial: example regular expression we're looking for is
/a^p_1|a^p_2|...|a^p_N/, where p_1, ..., p_N are primes below. Sadly, to use
it to test for primality, we need to know if the number is prime beforehand.

Actually, it sounds like an interesting problem -- to come up with an
interesting and nontrivial regular expression schema that for every N gives us
a regular expression that tests for primeness all numbers less than N.

Come to think of it, the existence of any such interesting schema seems highly
unlikely -- the Parikh's theorem implies that the set of lengths of words
matched by regular expression (or even context-free grammar) seems to be too
constrained to allow for such scheme -- we can easily find non-prime matched
by an such regular expression, and Parikhs theorem seems to imply that it is
not much bigger than the automaton state count.

------
state_machine
Beware: Depending on your backtracking limits, at some point the regex will
start deciding numbers are prime that aren't.

<http://zmievski.org/2010/08/the-prime-that-wasnt>

------
DanielRibeiro
Former incarnations of the same thing here on HN:

<http://news.ycombinator.com/item?id=1486158>

<http://news.ycombinator.com/item?id=58780>

~~~
its_so_on
that's what, five years now. Imagine what you would need to pay someone who
didn't know the code to come debug the current version.

That's Perl built-in job security right there. Also why I don't use it except
as throw-away scripting at the bash prompt:)

~~~
berntb
>>That's Perl built-in job security right there.

Huh? You mean, this is about Perl because regexps was created in Perl and the
Perl regexp lib isn't ported anywhere else?

Oh, wait...

(If you must troll, at least be interesting. :-( )

~~~
its_so_on
I'm sorry must have been drunk, and that one-liner is in reality easy to
debug, or even understand.

~~~
berntb
OK, that assumption that this is how serious people normally writes REs was at
least entertaining. But (still) not really contributing to the HN quality.

------
ricardobeat
First seen in 1998: <http://diswww.mit.edu/bloom-picayune.mit.edu/perl/10138>

------
eliben
Unless I'm missing something obvious, this post lacks a proper attribution. It
is an old, very old and very well-known one-liner that made rounds and rounds
in the Perl community over the years. Reading Krumins's post one might think
he invented it.

~~~
stevecooperorg
possibly added reently, but the last line of the post reads "PS. I didn't
invent this regular expression, it was invented in 1998 by Abigail."

~~~
eliben
Yep, it wasn't there when I first looked.

~~~
pkrumins
Yup, I updated the article so it didn't sound like I invented it. Also added a
message that it's not a real regular expression.

------
fxn
I wrote a coprimality test based on the same technique
<https://gist.github.com/1520171>

~~~
pkrumins
Pretty awesome!

