[Odd and strange] using regular expressions to determine prime numbers

Source: Internet
Author: User

I recently learned Regular Expressions and occasionally saw a post that uses regular expressions to determine whether a number is a prime number. At that time, I was shocked and felt amazing. The function for determining prime numbers is like this:

 
Public static bool isprime (int I) {return! RegEx. ismatch (new string ('*', I), "^ .? $ | ^ (... + ?) \ 1 + $ ");}

Do you think it's amazing? At that time, I felt quite imaginative. Let's take a look at how this regular expression judges the prime number.

    • The first step is to create a string with the length of I and filled.

    • Step 2: Let the regular expression next to this string match. If it matches, it is not a prime number.

The process is simple. Let's take a look at this regular expression.

If this regular expression matches, this number is not a prime number. This regular expression can be divided into two parts,

    • Part 1:^ .? $.^Start position of the matching string,$Match the end position of the string,.Match any character,?Optional. It means matching strings with only one character or without any character, that is, 0 or 1. 0 or 1 is not a prime number. Here the two numbers are matched.
    • Part 2:^ (... + ?) \ 1 + $. This part is critical. Let's split it to explain the meaning,^And$I will not explain it.(... + ?)Match two or more arbitrary characters.+?It indicates that it ignores the priority (lazy) and has no impact on the results, for performance consideration. Note that this expression has a bracket, which is a capturing bracket.\ 1Is a reverse reference. The referenced content is the content selected in the first bracket. Then\ 1 +Match the content in the matching brackets once or multiple times.

After the basic content of this expression is explained, why can it match non-prime numbers?

The matching process is roughly like this,(... + ?)First match 2 characters, then\ 1 +Match a multiple of 2 characters. If yes, does it mean that this number is a multiple of 2? When the matching fails, the matching engine(... + ?)Match three characters, and then\ 1 +It must be a multiple of three characters. If it can be matched, it must be a multiple of three. So on ......

If the number is matched, it indicates that the number must be divisible by a certain number. If the number is not matched after the match, the number from 2 to n-1 cannot be divisible by N, that proves that this number is a prime number!

 

Digress

(... + ?)Medium?I think this is to improve performance. If no?Words(... + ?)It will try to match all the characters, and then find there are expressions behind it, so it starts backtracking from the last position. And added?Then the matching starts from the second one.

For example, we match 12.^ .? $ | ^ (.. +) \ 1 + $We will first determine whether 12 is a multiple of 11 and then 12 is a multiple of 10... then 12 is a multiple of 6.^ .? $ | ^ (... + ?) \ 1 + $What we will do is first judge whether 12 is a multiple of 2, so then will not have then. The efficiency of prime numbers is the same, because each position needs to be matched, but for non-prime numbers, matching from an early stage can make this expression end early.

If you do not understand what I have explained, Let's explain it to others:

    • Coolshell
    • A regular expression to check for prime numbers

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.