15
I recently saw a publishing how they taught Perl to recognize a prime number using regular expressions. The regular expression in question is:
/^1?$|^(11+?)\1+$/
The only requirement of this regular expression is that the number be given in unary format (the publication shows how to make the transformation from a decimal number to unary in Perl).
Do you know what this "unary format" is? Well, unlike our usual decimal (or hexadecimal or binary), the unary is not positional.
In fact, if you have ever used your fingers to make an account or report a quantity, you have already used anointing. In this format, the number of symbols of an expression (or fingers raised from the hand) corresponds to the number in question. For example,
X
in unary is equivalent to decimal1
, andXXXXX
is equivalent to decimal5
.Any symbol can be used in the unary representation, and the most common is to use the character
1
. So if I want to represent the6
decimal in unary, I must repeat the symbol a total of six times; using the symbol1
, the representation of the six would be111111
How does this expression work? Well, it actually detects numbers that are not prime. It is divided into two parts:
^1?$
^(11+?)\1+$
The first part is responsible for picking trivially non-prime numbers: 0 and 1. The second part is more interesting, however.
In the second part, we have a term that is contained in a grouping (the 11+?
). Let’s leave him in the corner for now, okay? Then, it consists of a rearview mirror (the \1
), who does the exact match of the previous grouping. But, not satisfied with this, this same match comes followed by the Kleene cross, which indicates that the expression must appear at least 1 time and can repeat itself until infinitely many times.
So, if the grouped string has length 3, the regular expression will give match in strings of length 6, 9, 12 and so on. This is because the Kleene cross will ensure that the string as a whole will have the following length (for l
being the size of the grouping string):
l + k*l, k >= 1
So, in order for the number not to be prime, you need to ensure that l != 1
. Returning to the grouping, this is defined in 11+?
. Here the wedding occurs with any string of size 2 or more (the interrogation after Kleene’s cross is only to be less greedy possible and to run faster, it is not strictly necessary). So with that, we have to l >= 2
. So if that second part of the regular expression gives a match in the string, we have that the number passed is a composite number.
Thus, through an expression that transcends the regular languages, this expression was assembled that recognizes the primality (or, rather, the non-prinality) of a number. Note that mirrors and other forms of explicit memory do not consist of expressions mathematically regular. Behold plus about the subject.
So, is it possible to draw some deterministic finite automaton that can recognize if a number is prime? If yes, how is it? If not, what proof that this problem cannot be solved through regular language?
DFA is O(n), this would amount to a factorization algorithm O(n)? where n=number of unary digits.
– epx
Due to the closure of LR under the complement, it would be at least a decision version of the number compounds.
– Jefferson Quesado