JS Regular detection primes

Source: Internet
Author: User

I believe a lot of people should have read this article, the first time I saw it was 11, when I learned to read this question.
The original "Regular expression of the number of prime checks", in the article has explained how he judged, I will not wordy.
Let's say how JS implements it.

First of all to see what is prime, in the "prime-Baidu Encyclopedia," There are detailed instructions, but also said some of the methods of the prime, what 6n+1 law, etc...
None of these algorithms are what we're talking about, we're just talking about regular.

The regular in that article is /^1?$|^ (11+?) \1+$/
In fact, I feel that there is absolutely no need to detect 0 1 or the like, because according to Baidu Encyclopedia, the prime number is from 2 start.
So just add a judgment condition N < 2 is not a prime number, others are /^ (11+?). \1+$/ for verification,
Not only improves efficiency, but also prevents the error of passing in negative numbers.

Let's start by writing a function that iterates through n primes.

/** * ergodic Prime number * @param  {number} max traverse the prime number between 2-max * @return {Array}      returns 2-max all primes between */function prime (max) {    var Re = new RegExp (' ^ (11+?) \\1+$ '),//Detect prime number regular        str = ' 1 ',//based on current I generated corresponding numbers 1         i = 1,//counter        ret = [];//prime number Result set while    (Max > i++) 
   
    re.test (str + = ' 1 ') | | Ret.push (i);    return ret;} var arr = prime (+); Console.log (arr);
   

Found in the code var re = new RegExp (' ^ (11+?) \\1+$ ') why not directly write var re =/^ (11+?) Where's \1+$/?
Go back and turn down some of the regular articles I wrote and I'll find the answer.
The most traditional notation is often nested loops to traverse primes, but with regular help, the code is simple and elegant.

If you want to judge the prime number, it is more concise.

/** * Determines whether a prime number * @param  {#} n  is to be judged as a digit * @return {Boolean}   returns true|false */function isprime (n) {    return N < 2? False:!/^ (11+?) \1+$/.test (Array (n + 1). Join (' 1 '));} Console.log ( -2, IsPrime ( -2)), Console.log (1, IsPrime (1)), Console.log (2, IsPrime (2)), Console.log (One, IsPrime (11));

Well, today's share of this thing, although no practical use, but can broaden everyone's horizons.
The regular is not only the match string, but also can judge the prime number, two Yuan/ternary equation has no solution and so on and so on seemingly incredible logic.

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.