The regular expression is as follows:
Copy codeThe Code is as follows:
^ 1? $ | ^ (11 + ?) \ 1 + $ can be used to determine the prime number (in the form of n to 1, where n is the size of the number. For example, convert 5 to 11111, 3 to 111, and 2 to 11 .)
What is a prime number?
Junior high school. What our teacher taught us at the beginning was "prime number ". See the following concepts:
Prime number. In a natural number greater than 1, except for 1 and the integer itself, it cannot be divisible by other natural numbers.
In other words, only the natural numbers of two positive factors (1 and itself) are prime numbers. A number larger than 1 but not a prime number is called a union number. 1 and 0 are both non-prime numbers and non-composite numbers.
What does this regular expression mean?
[^ 1? $ | ^ (11 + ?) \ 1 + $] separated by [|. [|] In the regular syntax, it indicates "or", acting on the first two units. (Skip the following section if you still don't understand it)
Copy codeThe Code is as follows:
For example, [AB | cd] can match "AB" or "cd", meaning that "AB" is "bc ", to match "abd" and "acd", modify the scope of [|] and add a range.
Change to a (B | c) or (? : B | c) d (the matching results are not grouped, which is more efficient ).
Continue with the regular expression, which is divided into two branches: [^ 1? $ And [^ (11 + ?) \ 1 + $ ]. In the regular syntax, the [^] delimiters indicate the beginning except in [[], and are not in brackets.
The first branch [^ 1? $] Matches "1" or "" (Null String ).
The second branch [^ (11 + ?) \ 1 + $. Check [(11 +?)] in the brackets ?)] Match the character "1" followed by [1 +], which is 1 to countless 1. 【?] The question mark indicates that it is not greedy, that is, matching as few as possible.
Next, let's look at [\ 1 +]. [\ 1] indicates referencing the results of the first matched group. That is, the matching result of the first [()] parentheses. Similarly, [\ 2] is the result captured by the second bracket. (Note 【(? :) It is written in no combination with the group. In this way, it cannot be referenced ])
[+] Is one to countless. We can see this expression. [(11 + ?)] It is regarded as 1 + n in mathematics, where n is a positive integer greater than 0. The '\ 1 +' outside refers to the number of times the group was referenced. It is interpreted as m times, where m is a positive integer greater than 0.
The entire expression is (1 + n) * m. Because n and m are all greater than 0, then 1 + n must be greater than 1, the minimum is 2, the maximum is infinity, the minimum m is 1, and the maximum is infinity.
Then, any multiples of a positive integer greater than 2 will always be the sum, which is a non-prime number.
Let's look at this expression. Match 0 or 1 string "1", that is, the number 0 and the number 1. And all others. The entire expression. If the expression is successfully matched, it is a non-prime number. If the expression is not matched, It is a prime number. That's right.
Copy codeThe Code is as follows:
If (preg_match ('/^ 1? $ | ^ (11 + ?) \ 1 + $/I ', $ subject )){
# Not a prime number
} Else {
# Is a prime number
}
TIPS: whether this method is used to identify the prime number is only for research and use. It cannot be used in formal programs. If the string is too long, it will cause a great fear of backtracking.
Http://blog.stevenlevithan.com/archives/algebra-with-regexes blog address:
In the previous blog, we mentioned two equations and regular expressions. Let's study them together.
• The expression [^ (. *) \ 1 {16} (. *) \ 2 {11} $] of the binary equation 17x + 12y = 51 ]. It's easy to understand. [(. *)] Is a number ranging from 0 to countless. (This is followed by the above. In fact, [.] point number indicates the character "1 ")
That is, 0 to countless 1, followed by [\ 1] reference once. The next [{16}] is 16 times. Act on the previous [\ 1], that is, 16 references. In addition, the initial [(. *)] is exactly 17 times. The other one will not talk about it, just like this one.
The RegEx engine tries 【(. *): attempts to add 0 to countless characters including "1", 0 characters "1", 1 character "1", and 2 characters "1. Otherwise, the maximum number of all characters "1" (51 characters "1") must be tried ").
• Equation 11x + 2y + 5z = 115, the expression of which is [^ (. *) \ 1 {10 }(. *) \ 2 {1 }(. *) \ 3 {4} $. This is the same as the above one. Note that [\ 2] and [\ 3] are worth 2nd and the content captured in 3rd parentheses.
------- Split line ------
The above interesting mathematical questions are to convert the integer to the corresponding number of characters "1 ". The following is the binary number.
Eat first and then write later.