Check the regular expression share _ regular expression of prime numbers

Source: Internet
Author: User
Tags expression engine
The regular expression looks like this:

Regular expression to check prime numbers or not

To use this regular expression, you need to turn the natural number into 1 strings, such as: 2 to write "11", 3 to write "111", 17 to write "11111111111111111", this work with some scripting language can be easily completed.

At first I was skeptical about the expression, but I looked at the expression and found it to be very reasonable, so let me take you to dissect the work of this expression.

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

    • The first part:/^1?$/, this part believes that I do not have to say more, its expression matches "empty string" and string only one "1" of strings.
    • Part II:/^ (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, matches a string that starts with "11" and repeats 0 or N 1. The latter part means to match the first half as a string to the remaining strings 1 or more times (this means--the number of 1 of the remaining string should be the integer multiple of 1 digits in the preceding string).

It is shown that the regular expression is to take a non prime number, to get the prime number must also be the entire expression negation. Through the above analysis, we know that the second part is the most important, for the second part, give a few examples,

Example One: Judge the natural number 8. We can see that 8 turns into our format is "11111111", for (11+?), it matches "11", so there is still "111111", and \1+$ exactly matches the remaining "111111", because, "11" This pattern occurs three times in "111111", matches the pattern match, and returns true. So, the match succeeds, so this number is not prime.

Example Two: Judge the natural numberone by one. The format we need is "11111111111" (11 1), for (11+?), it matches "11" (the first two 1), and "111111111" (nine 1), and \1+$ cannot match that "11". Nine 1 ", because the" 11 "pattern does not appear exactly n times in the" Nine 1 "string. So our regular expression engine will try the next method, first match "111" (first three 1), then "111" as a pattern to match the remaining "11111111" (eight 1), it is obvious that "eight 1" does not match "three 1" multiple times. So the engine will continue to try it down ... Until the attempt all may not match successfully. So 11 is prime.

By example two, we can get the equivalent number algorithm, the regular expression will match the number of 1 in which there is no "two 1" integer times, "Three 1" integer times, "Four 1" integer times ..., and, this is exactly what we need to calculate the prime number of the algorithm. Now, you see.

Below, we use Perl to keep outputting prime numbers with this regular expression: (I don't say much about Perl syntax, please note the inverse operator before the expression)

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

In addition, let's extrapolate that, according to the above method, we can even use regular expressions to verify whether there are solutions in a certain way, such as:

    • two-Yuan equation : 17x + 12y = 51 A regular expression that determines whether it has a solution is: ^(. *)\1{16}(. *)\2{11}$
    • ternary equation : 11x + 2y + 5z = 115 A regular expression that determines whether it has a solution is:^(. *)\1{10} (.*) \2{1} (.*) \3{4}$

You might as well do your own exercises, why the above two regular expressions can determine whether the equation has a solution. If you can't fathom the mystery, you can read this article in English.

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.