Live data from Hacker News

Regular Expression That Checks If A Number Is Prime

iluxonchik.github.io

11–20 of 117 posts

Re: Regular Expression That Checks If A Number Is Prime

#11
post #6

tl;dr if you know a little regex - convert your number to a string of that length (1 = 1, 2 = 11, 3 = 111) - handle 0 / 1 separately - ^(..+?)\1+$ - trick; the WHOLE thing has to match to return (^$) - first \1 match is "11", ergo string must be "11", "1111", "111111" to match - second \1 match "111", ergo string must be "111", "111111", "111111111" to match - and so on. if you find before length of string, it was no…

"clever trick" is something we explicitly avoid asking these days.

Re: Regular Expression That Checks If A Number Is Prime

#12
post #7
post #6

tl;dr if you know a little regex - convert your number to a string of that length (1 = 1, 2 = 11, 3 = 111) - handle 0 / 1 separately - ^(..+?)\1+$ - trick; the WHOLE thing has to match to return (^$) - first \1 match is "11", ergo string must be "11", "1111", "111111" to match - second \1 match "111", ergo string must be "111", "111111", "111111111" to match - and so on. if you find before length of string, it was no…

Doesn't this have some relation to https://en.wikipedia.org/wiki/Church_encoding

It's basically a prime sieve done on a string encoding.

Re: Regular Expression That Checks If A Number Is Prime

#13
post #10
post #8

Earlier quoted context omitted.

That's true, but it is actually possible to construct true regular expressions that check if a binary string of digits is divisible by an arbitrary integer. Obviously not the quickest or most intuitive way to do it, though. There's an interesting book that covers some unusual aspects of automata and regular expressions: https://www.amazon.com/Finite-Automata-Regular-Expressions-S...

Sure, for a fixed arbitrary integer M. (Supposing the input is fed in in big-endian order, you just keep track of the remainder modulo M as each new bit comes in, updating it by turning r into (r * 2 + new bit) mod M. Since this only requires finite state, it is regular). But that won't get you a primality checker. You can't get a primality checker. Primes don't comprise a regular language, neither in unary nor in an…

[deleted]

Re: Regular Expression That Checks If A Number Is Prime

#14
It's basically the Sieve of Eratosthenes[1] algorithm, and it's possible to do it with regular expressions because numbers here are represented in unary[2] number system, where number of characters/tokens equals the number itself. It's a common trick for testing various Turing-machine stuff.

[1] https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes [2] https://en.wikipedia.org/wiki/Unary_numeral_system

Re: Regular Expression That Checks If A Number Is Prime

#15
post #3

It should be noted that this is not possible with regular expressions in the traditional sense (i.e. regular expressions only matching regular languages): http://math.stackexchange.com/a/181233/21405 Because of back references PCRE regular expressions can match non-regular languages as well.

One of my pet peeves is how PRCE destroyed the definition of "regular" in regular expressions. It has basically made a huge number of programmers illiterate as to what truly is regular in the formal sense.

Re: Regular Expression That Checks If A Number Is Prime

#16
post #2

Very interesting. Thanks for writing this up. This is a bit more Perlish (not that I'm an authority): sub is_prime { (1x$_[0]) !~ /^(?:.?|(.{2,}?)\1+)$/; } But perhaps less clear to a novice reader. You can also leave off the semicolon.

Thank you :)

You are probably right, but this function was my first head-to-head encounter with Perl, I researched just a little bit what I needed to know in order to write it.

Re: Regular Expression That Checks If A Number Is Prime

#18
post #5

> How would we go about that? Well, all we have to do is add ? in front of the +. This will lead us to the regex. I was very confused until I realized the author's definition of 'in front' wasn't the same as mine...

What do you mean? Could you please clarify that? What did your understand by "in front"?

Re: Regular Expression That Checks If A Number Is Prime

#19
post #5

> How would we go about that? Well, all we have to do is add ? in front of the +. This will lead us to the regex. I was very confused until I realized the author's definition of 'in front' wasn't the same as mine...

What do you mean? Could you please clarify that? What did your understand by "in front"?

Presumably, they thought of A as in front of B in the expression "AB" rather than in the expression "BA".

(Which is also how I would normally think of "in front", for what it's worth!)

Re: Regular Expression That Checks If A Number Is Prime

#20
post #4

I wish they'd stop calling these expressions "regular".

Okay, technically it's regex, but I didn't want to get into this distinction, since "regex" and "regular expression" are used pretty much interchangeably (unless you're in the academia :) ).
Post reply on HN