The optimal algorithm for calculating prime numbers by Sieve method + explanation

Source: Internet
Author: User

Sieve method to calculate prime number:the number of primes in n is evaluated. First use 2 to sift, that is, 2 left, the number of 2 is removed, and then the next prime number, that is, 3 sieve, 3 left, the multiples of 3 is removed, then the next prime number 5 sieve, 5 left, the multiples of 5 are removed;

Thus, we can write the basic version of the Sieve method to seek the vegetarian:
Const intMAXN =102410240;BOOLISP[MAXN];voidinit () {memset (ISP,true,sizeof(ISP)); isp[0] = isp[1] =false; Const intMax1 = sqrt (MAXN +0.5);  for(inti =2; I <= max1; i++)        if(Isp[i]) for(intj = i * I; J < Maxn; J + =i) isp[j]=false;}
After testing, the number of primes within 100 million is estimated to be more than 2.7s. But we have found that the algorithm can be optimized because there are a lot of composite that are labeled repeatedly. For example, 40 were marked by prime 2,5. The following optimized version of the algorithm can mark all the numbers only once, so that the algorithm close to O (n)
Const intMAXN =102410240;BOOLNOT_PRIME[MAXN];intprime[maxn/2], CNT;voidinit () {memset (Not_prime,0,sizeof(Not_prime));  for(inti =2; i < MAXN; i++) {        if(!not_prime[i]) prime[cnt++] =i; Location 1 for(intj =0; J < cnt && I * prime[j] < MAXN; J + +) {Not_prime[i* Prime[j]] =1; Location 2if(! (i% prime[j])) Break; Location 3        }    }    }

After testing, the prime number within 100 million can be seen as long as 1.43 seconds faster than the basic version (my little Mac runs fast)

Explanation: So how does this algorithm ensure that each number is marked only once?  Here's a cursory explanation of how I understand it: for all integers greater than 1, they are divided into prime numbers (primes) and composite. Composite can be divided into two types: 1. Consists of a prime number and another 1 prime numbers of 2. Made up of more than 2 prime numbers---> further consider it as: a prime number and a composite in position 1 we can get all the prime numbers, and I don't have to explain that. In position 2 we mark all composite: At the time of position 2 altogether by two kinds of possible: 1. I for prime numbers, prime[j] for prime numbers can see exactly the first kind of composite, so all the first composite will be labeled 2. I for composite, prime[j] for prime numbers exactly in accordance with the second kind of composite, all the second kind of composite will be marked OK, we now know that the algorithm will be all composite marked out, then it is not repeated tag? Depending on position 3 because any one compositeaCan be broken into a prime number smaller than it.bMultiplied by a composite smaller than itC(emphasizing "smaller than it") so inI<a, there must have been a time when the point wasIEqualsCAndPrime[j]Equalsb, and this combination is unique , then exit when they're marked. Because each composite has a unique set of A, B, thisensures that the algorithm is not heavy.

The optimal algorithm for calculating prime numbers by Sieve method + explanation

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.