289 points · by graderjs · 4y ago · 121 comments
(I have no clue whether unary-representation primality is actually regular or not -- it's been far too long since my automata class to remember how to (dis)prove that -- but I guess it's at least sublinear, since primality is polynomial w/r/t the logarithm of the number being tested.)
[1] https://en.wikipedia.org/wiki/Regular_expression#Formal_defi...
{1^x : x is prime} is not a regular language. suppose it is a regular language. then by the pumping lemma there exists a p such that every string of length at least p can be written as w = xyz, where |y| >= 1, |xy| <= p [not an important observation for this proof], and we can "pump" a new string w' = xy^nz (for any n > 0) and have w' remain in L.
let p' be the prime immediately above p. then the string
w = 1^p'
is in the language L and must be "pumpable" for some N = |y| > 0. Choose the number of times we "pump" (n in the expression xy^nz) to be p'. Then that would imply
w' = 1^(p' + p' * N)
is in the language L, a contradiction, as p' + p' * N has factors p' and (1 + N). Note we can also use the CFL pumping lemma to prove that this particular language is not context-free either; if we choose s = uvwxy, we can still just pump p' times; the new string just is of size p' * (1 + |v| + |x|), and |v| + |x| > 0.
While this is certainly true, I would prefer to note that n ≥ 0. n > 0 is just a special case of that.
(You can see clearly that 0 repetitions will always be valid by considering the proof of the pumping lemma: since a regular expression is recognized by a finite state machine, the only way to recognize a string of length exceeding the (finite!) number of states in the machine is to visit some states more than once, which means traveling around a cycle in the state graph. The 'pumpable substring' of the pumping lemma is a path through such a cycle, and repeating it zero times corresponds to traveling around the cycle zero times. The rest of the path is just as valid as always.)
The language {1^x : x is not prime} is easily pumped with x=z=1 and y=11. And regular languages are closed under complement.
No pumping Lemma -> Not regular, Yeah Pumping Lemma -> Who knows
1. The language {1^x: x prime} is not regular (as immediately shown by the pumping lemma).
2. The language {1^x: x not prime} is not regular (because it is the complement of {1^x: x prime}, and the complement of a regular language is regular).
It follows that the pumping lemma does not apply to the language {1^x: x not prime}; just because the string 1111 can be divided that way doesn't mean all strings in the language can be so divided.
> No pumping Lemma -> Not regular, Yeah Pumping Lemma -> Who knows
This is incorrect; any language to which the pumping lemma for regular languages applies is a regular language.
Good. There's that obligative conversation over and done with.
Not to mention the plethora of mostly-but-not-totally compatible regexp languages which exist. C++ supports 6 [1], and that doesn't even include PCREs. Miscommunication about which language one is actually using is a common source of regexp bugs.
[1] https://en.cppreference.com/w/cpp/regex/syntax_option_type
same boat. that's why we use re2 in a way inspired by Go (which means losing a few features but the tradeoff is worth it):
> The regexp implementation provided by this package is guaranteed to run in time linear in the size of the input. (This is a property not guaranteed by most open source implementations of regular expressions.) For more information about this property, see
> https://swtch.com/~rsc/regexp/regexp1.html
> or any book about automata theory.
Wow, TIL. I always thought C++ had evolved into an excessively complex language, but this really seems to take the cake. ECMAScript (with C++-only changes), basic POSIX, extended POSIX, POSIX awk, POSIX grep, and POSIX extended grep. You are right there is no PCRE in there, yet. (There's always C++23... and if not that, C++26 or C++30)
(Although ECMASCript regex was heavily inspired by Perl/PCRE anyway, albeit without being 100% compatible with either, so in a sense PCRE is already there.)
If you're sloppy with words you probably have other nasty habits. (apologies to Heinlein)
There are reasons to employ regular languages with extensions (to not quite context free languages) but be clear about what you're talking about.
I personally prefer using "regular expression" in the language-theoretic sense and "regex" or "regexp" for related concepts in programming languages and software.
This is also what Larry Wall proposes:
> [...] Pattern Matching, generally having to do with what we call "regular expressions", which are only marginally related to real regular expressions. Nevertheless, the term has grown with the capabilities of our pattern matching engines, so I'm not going to try to fight linguistic necessity here. I will, however, generally call them "regexes" (or "regexen", when I'm in an Anglo-Saxon mood).
Demonstrating the power of a regex engine by solving a problem like this is interesting and very clever, but it’s hopefully not used in production.
The RE2 library seems to:
>loosely speaking, a regular language is a set of strings that can be matched in a single pass through the text using only a fixed amount of memory.
https://github.com/google/re2/wiki/Syntax
Note that RE2 doesn't support backreferences.
Your average coding bootcamp kid cracking away in node and invoking the builtin regex engine has no idea what any of those words mean.
To those programmers the words "regular expression" have become completely separated from their original etymology in lexical analysis. To them a regex means "input to a regex engine", and thus the term "use a regular expression" means "use a regex engine". It could be "use a froofroo boingo", for all the meaning the words hold. "Regular expression" just became the vernacular due to its origination in lexical analysis.
https://blog.cloudflare.com/details-of-the-cloudflare-outage...
My initial reaction to the title was "wait, you can describe prime numbers using formal languages?". I could feel it in my guts that this would probably have some mind blowing Math/CS implications.
A shame how I'm so rusty in my Mathematics and CS nowadays :(
DFA is the absurdity, not NFA.
Every non-deterministic finite state automaton can be converted into a deterministic finite state automaton that accepts the same language (see powerset construction [1]), so I am not sure what you are saying here.
eg tons of simple recursively defined languages were converted to DFA monstrosities, and since then they became slower and unmaintainable.
"It is important because NFAs can be used to reduce the complexity of the mathematical work required to establish many important properties in the theory of computation. For example, it is much easier to prove closure properties of regular languages using NFAs than DFAs." https://en.m.wikipedia.org/wiki/Nondeterministic_finite_auto...
but more importantly look at the various attempts to the solve backtracking "problem" (stack overflow by unbounded recursion), as done recently in perl and pcre, which led to more insecure AND more unmaintainable regex code by converting their simple recursion to iterations on the heap. first it got much slower, as the state is now handled on the heap, and second it got much more insecure as DOS attacks can now exhaust the whole heap, not just around 1024 stack depths. and third, it got 10x more complicated, and unreadable.
ad language: you can of course define a language by forbidding left recursion, which yacc folks would prefer. but you can define it much easier with left recursive parsers, like 10x smaller. of course you can always expand simple NFA's to deterministic DFA's at the cost of exponential state explosion and increased complexity. but it is a monstrosity only impractical CS folks prefer. SW engineers prefer the similar, faster and more secure NFA backtracking versions, as long as you count the stack depth.
It has been discussed a few times on Hacker News, Reddit, etc. over the past 15 years...
https://www.masteringperl.org/2013/06/how-abigails-prime-num...
This blog article does a nice job discussing Abigail’s work.
No. This won't work because the time cost of primality testing is too high (cubic in the number of binary digits?), and checking if a string satisfies a regular expression takes only linear time. Since cubic is bigger than linear, this won't work. Too good to be true!
Not a proof, but a rule of thumb. No need to even use the pumping lemma.
This is just a conjecture, though.
> No need to even use the pumping lemma.
But it's really easy to prove this with the pumping lemma. This could be the first problem in a textbook section that introduces the pumping lemma. It would be difficult to come up with a language for which the pumping lemma disproof was more obvious.
-----
(1) a^n b^n
The pumping lemma tells us that for some string in this language, we can divide the string into three substrings x y z such that x y^n z remains in the language for all n.
There are two cases:
(1)(a) The number of 'a' characters in y does not equal the number of 'b' characters. In this case, pumping y will violate the constraint that a string in the language must have an equal number of 'a's and 'b's.
(1)(b) y consists of k 'a's followed by k 'b's for some k > 0. In this case, pumping it will violate the constraint that for every string in this language, each 'a' character must precede every 'b' character.
Thus, the language a^n b^n is not regular.
-----
(2) a^n, where n is prime
The pumping lemma tells us that for some string in this language, we can divide the string into three substrings x y z such that x y^n z remains in the language for all n. [same as above]
But the string x y^|xz| z has length |xz| + |y|*|xz|, which is not prime.
Thus, the language a^n (where n is prime) is not regular.
-----
Which of those was easier?
So it actually 'works', it's just slower.
Nope, it's just playing with lengths.
- then trying to simulate divisibility test by trying to find perfect groups of '11' to simulate division by 2 failing which (that is the backtracking part) and trying find perfect groups of '111' to simulate division by 3 and so on...
Clever but very inefficient
#!/usr/bin/env ruby
def is_prime(n)
("1" \* n.to_i)!~ /^1?$|^(11+?)\1+$/
endnum = ARGV.shift
$stdout.puts is_prime(num)
... then decided to test it on a button-mashed number:
$ruby ~/prime.rb 767423
... and it hung.
Thought it was the length of the number, so I tried something smaller (101). Quick "true". Then I tried progressively more digits. I got to `ruby ~/prime.rb 7673121` (false) and it took less than a second.
But it doesn't like something about the number 767423.
101 is prime, but it does "only" a hundred checks approx.
7673121 is divisible by 3, so as soon as it tries to match groups of 3 it succeeds and stops.
767423 is prime, so it needs to do hundreds of millions of checks, that's probably why it hungs.
Basically: the runtime of the algorithm should be equivalent to the minimum divisor excluding 1 of the number.
Which is a shame because it's really easy to simplify away most of this regex.
/ does nothing
^ and $ only exist to make it do the whole string
| is just saying "or", and only the part to the right really matters.
And the ? doesn't do anything meaningful.
Throw all that out and all you have is (11+)\1+
It's still a mash of symbols, but it's much simpler.
And changing it to (cc+)\1+ would probably help naive readability too.
Better explanations in http://neilk.net/blog/2000/06/01/abigails-regex-to-test-for-...
Abigail's other regular expression hacks https://metacpan.org/author/ABIGAIL?sort=[[3,1]]
And a regex to integer-divide numbers by the square root of 2: https://codegolf.stackexchange.com/a/198428
Literally what its doing is turn a number in to all 1s, if the number is 1 return true. Otherwise try all possible ways to put it into 2 or more equal groups, return true once we find one that works.
You don't get closer to the definition of "prime" than that.
^(11+)\1+$
it does not handle "1" for clarity. I also don't understand why author used `+?`, because simple `+` should work just as well. At least it works in Idea for me.I suspect that since a factor must be smaller than the square root of a composite, the greedy version you proposed would waste a lot of time checking numbers which could never be factors of the composite under test.
If the number is prime, I think both variants have the same performance because they will exhaustively evaluate all numbers less than or equal to the composite under test.
Edit: this is not quite correct. One of the prime factors must be smaller than the square root of the composite numbers, but the other one can obviously be larger. For example: 2 * 17 = 34. I’m leaving the original statement as is, because I’m hoping someone with a better math background can chime in and explain if the way that prime factors are distributed is symmetrical or not.
it is basically trying to group by 11 (divide by 2), if there no match (via backtracking) group by 111 (divide by 3) and so on
not very efficient by any stretch esp with string matches.
- it is turning the number into a string of 1s 3 => 111 4 => 1111 and so on
- then trying to simulate divisibility test by trying to find perfect groups of '11' to simulate division by 2 failing which (that is the backtracking part) and trying find perfect groups of '111' to simulate division by 3 and so on...
Clever but very inefficient
> require "prime"
=> true
> 10.prime?
=> falseWhy? The method works. Size doesn't matter. But it's extremely inefficient. Which can be calculated rather than measured.