
The Prime That Wasn’t - fogus
http://zmievski.org/2010/08/the-prime-that-wasnt?
======
bediger
That's not a true Regular Expression: it uses back-tracking, and to the
author's credit, he notes that in the article.

True regular expressions can't recognize primes. It's important to keep these
kind of distinctions in mind for two reasons.

First, without clear, precisely defined categories of formal languages, you'll
fail to recognize unsolvable (or undecidable) problems and waste a lot of
time. Personally, I put "malware dectection" in this category, as determining
whether a given file constitutes a virus seems equivalent to solving the
halting problem.

Second, sloppy usage like using "Regular Expression" for something else is one
of those common usage issues that allow people to argue based on the name
("regular expression") to lure the unsuspecting in, but then fall back on
extreme technicalities to trick people into agreeing with the argument when
they might not otherwise have agreed. A lot like I've done in this comment.

~~~
eru
To see some genuine regular expression trickery, see
<https://github.com/matthiasgoergens/Div7/blob/master/regex7> for a regular
expression that recognizes decimal numbers that are divisible by 7.

If you memorize that expression, you have a handy way to tell whether a number
is divisible by 7.

~~~
shasta
It's much easier to remember and apply the corresponding finite state machine,
where you just keep track of the remainder mod 7 that you've seen thus far.
Example: 48193. What's the remainder mod 7?

48 mod 7 = 6 -- First two digits

61 mod 7 = 5 -- Remainder thus far, appending next digit

59 mod 7 = 3 -- Remainder thus far, appending next digit

33 mod 7 = 5 -- Remainder thus far, appending next digit

No more digits, so the answer is 5.

(Edit: I did figure that you were almost certainly being sarcastic. I just
wanted to point out that in its corresponding FSM form, we've memorized the
transitions as children.)

~~~
pronoiac
You're doing all the work of dividing by 7. There are shortcuts to checking
divisibility [1] - 2, 3, 5, & 9 have the easiest shortcuts - but 7 is a
complicated case, which I'd probably just divide out anyway.

[1]
[http://en.wikipedia.org/wiki/Divisibility_rule#Divisibility_...](http://en.wikipedia.org/wiki/Divisibility_rule#Divisibility_by_7)

~~~
eru
No, he's not doing the work of dividing by seven. That would need memory of
around the same size as your number of digits of your input number. His state
machine only reads one digit at a time and only needs to store a less-than-
one-digit state.

My regular expression is a joke on the usual divisibility rules.

By the way, you can find your own divisibility rules. They are actually easy
to find and prove, if you know a bit about cyclic groups / calculating with
modulo. E.g. the rule for divisibility by F in hexadecimal is the same as the
one for 9 in decimal.

Divisibility by 11 or 101 or 1001, or 9, 99, 999 is also really simple.

------
ars
Dup: <http://news.ycombinator.com/item?id=1572031>

Did you add a question mark at the end to avoid the dup checker?

~~~
ugh
That Submission is more than a year old. As far as I know, the dup checker
doesn't check back that far.

~~~
Herald_MJ
It does. I came across this article yesterday morning and attempted to submit
it to HN, found that it was rejected for being a dupe.

Ultimately, I submitted it to reddit.com/r/compsci instead. I assume fogus
picked up on it from there and hacked the HN dup detector by appending a '?'
to the URL. Kind of annoying, really.

------
jarin
It kind of reminds me of this blog post from a while back about Apple's
CFArray implementation and how it switches data structure implementations at
about 300,000 elements:
<http://ridiculousfish.com/blog/archives/2005/12/23/array/>

Just a reminder that the simpler things seem, the more complex they can often
get under the surface.

~~~
seles
The regex is not complex, just the php implementation is.

I feel like any regex expression should give an error or at least a warning if
is going to return a potentially incorrect answer.

~~~
bobfunk
Would absolutely expect an exception if my regex engine decided it couldn't
handle an expression!

Just giving back a wrong result unless you tweak the php.ini file seems
insane. Stuff like this is really what makes PHP deserve its bad rep...

------
makmanalp
> Needless to say, don’t use this prime determination algorithm for any sort
> of real-life stuff. Instead, just appreciate it for its elegance.

Yet another testament to the subjectivity of elegance in code. One man's
elegant code is another's ugly hack. (Not to discredit the cleverness of the
author.)

------
drivebyacct2
I don't find solutions that are limited in success and are ill-adapted to the
problem set to be "elegant", but maybe I'm nitpicking.

