Check Regular Expression sharing of prime numbers

Source: Internet
Author: User
Tags expression engine

This regular expression is shown in:

Regular Expression for checking the prime number or not

To use this regular expression, you need to convert the natural number into multiple strings of 1. For example, 2 must be written as "11", and 3 must be written as "111 ", 17 "11111111111111111" should be written, which can be easily completed using some scripting languages.

At first, I was skeptical about this expression, but I carefully studied this expression and found it very reasonable. below, let me show you how this expression works.

First, we can see that the expression contains "|", which means the expression can be divided into two parts:/^ 1? $/AND/^ (11 + ?) \ 1 + $/

  • Part 1:/^ 1? $/I don't need to say much about this part. It indicates matching "empty string" and only one "1" string in the string.
  • Part 2:/^ (11 + ?) \ 1 + $/This part is the key part of the entire expression. It can be divided into two parts,(11 + ?)And\ 1 + $, The first half is very simple. match strings starting with "11" and repeat 0 or n 1, the latter part means that the first half is used as a string to match the remaining string once or multiple times --The number of the remaining strings 1 is an integer multiple of the number of the preceding strings 1.).

It can be seen that this regular expression is a non-prime number. To obtain a prime number, you must reverse the entire expression. Through the above analysis, we know that the second part is the most important. For the second part, let's give a few examples,

Example 1: Determine the natural number 8. We can know that 8 is converted into our format as "11111111".(11 + ?)It matches "11", so there is still "111111", and\ 1 + $Exactly match the remaining "111111", because the "11" pattern appears three times in "111111". If the pattern matches, true is returned. Therefore, the match is successful, so this number is not a prime number.

Example 2: Determine the natural number 11. To convert to the format we need is "11111111111" (11: 1).(11 + ?), It matches "11" (the first two 1), and there is still "111111111" (nine 1), while\ 1 + $We cannot match the "nine ones" for "11", because the "11" mode does not appear N times in the "nine ones" string. Therefore, our regular expression engine will try the following method, First Matching "111" (first three 1 ), then we use "111" as the pattern to match the remaining "11111111" (eight 1). Obviously, "eight 1" does not match "Three 1" multiple times. So the engine will continue to try again ...... All attempts may fail to match. Therefore, 11 is a prime number.

Through example 2, we can obtain such an equivalent number calculation algorithm. The regular expression will match the number of integers that show "Two 1, an integer multiple of "Three Ones" and an integer multiple of "Four Ones ......, However, this is exactly the algorithm we need to calculate the prime number. Now everyone understands.

Next, we will use perl to use this regular expression to continuously output prime numbers: (I will not talk much about the perl syntax. Please pay attention to the anti-operator before the expression)

Perl-e '$ | ++; (1 x $ _)!~ /^ 1? $ | ^ (11 + ?) \ 1 + $/& print "$ _" while ++ $ _'

In addition, let's make a difference. Based on the above method, we can even use a regular expression to verify whether a certain method has a solution, for example:

  • Binary Equation: 17x + 12y = 51 The regular expression used to determine whether a solution exists is:^(.*)\ 1 {16}(.*)\ 2 {11} $
  • Ternary Equation: 11x + 2y + 5z = 115 the regular expression used to determine whether a solution exists is:^(.*)\ 1 {10}(.*)\ 2 {1}(.*)\ 3 {4} $

You may wish to do the exercises on your own. The two regular expressions mentioned above can determine whether the equation has a solution. If you cannot understand the details, you can read this English article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.