Regular expression sharing for checking primes

Time:2021-1-19

This regular expression is shown as follows:

Regular expressions for checking prime numbers or not

To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of work can be easily completed by using some scripting languages.

At first, I was skeptical about this expression, but after careful study of this expression, I found that it is very reasonable. Now, let me take you to analyze the working principle of this expression in detail.

First of all, we can see that the expression has “|”, that is to say, the expression can be divided into two parts: / ^ 1? $/ and / ^ (11 +?) – 1+$/

  • Part one: / ^ 1$/I don’t think I need to say more about this part. It means to match “empty string” and a string with only one “1” in the string.
  • Part 2: / ^ (11 +) / 1+$/This part is the key part of the whole expression. It can be divided into two parts,(11+?)and\1+$The first half is very simple. It matches a string that starts with “11” and repeats 0 or n ones. The latter part means to match the remaining string one or more times as a string——The number of 1 of the remaining string is an integral multiple of the number of 1 of the preceding string)。

It can be seen that the positive regular expression is a non prime number. To get a prime number, we have to reverse the whole expression. From the above analysis, we know that the second part is the most important. For the second part, let’s take a few examples,

Example 1: judge natural number 8. We can see that 8 is converted to our format as “11111111”(11+?)It matches “11”, so “111111” is left, and\1+$It just matches the remaining “111111”, because the pattern “11” appears three times in “111111”, which conforms to the pattern matching and returns true. So, the match is successful, so the number is not prime.

Example 2: judge natural number 11. The format we need is “11111111” (eleven ones)(11+?), which matches “11” (the first two ones), leaving “111111111” (nine ones), and\1+$We can’t match the “nine ones” for “11” because the “11” pattern doesn’t appear exactly n times in the “nine ones” string. Therefore, our regular expression engine will try the next method, first match “111” (the first three ones), and then use “111” as a pattern to match the remaining “11111111” (eight ones). Obviously, the “Eight Ones” do not match “three ones” many times. So the engine will continue to try down Until the attempt all may not match successfully. So 11 is prime.

Through example 2, we can get such an equivalent arithmetic. Regular expressions will match whether there are integer multiples of “two ones”, “three ones” and “four ones” among these ones And this is exactly the algorithm we need to compute prime numbers. Now you understand.

Next, we use Perl to use this positive regular expression to continuously output prime numbers: (I won’t say more about the syntax of Perl, please pay attention to the negation operator before the expression)

perl -e’$|++;(1 x$_)!~/^1?$|^(11+?)\1+$/&&print”$_ “while ++$_’

In addition, let’s draw inferences from one instance. According to the above method, we can even use regular expressions to prove whether there is a solution in a certain way, such as:

  • Binary equation: 17x + 12Y = 51 the regular expression to judge whether it has a solution is:^(.*)\1{16}(.*)\2{11}$
  • Ternary equation: 11x + 2Y + 5Z = 115 the regular expression to judge whether there is a solution is as follows:^(.*)\1{10}(.*)\2{1}(.*)\3{4}$

Why can the above two regular expressions judge whether the equation has a solution. If you can’t understand the mystery, you can read this English article.