Primes, different prime numbers, all sorts of problems are always encountered in prime numbers.
Let's consider a more efficient algorithm for the prime number.
is the Sieve method. Because this requires the 1-n interval of the prime number only need O (NLOGLOGN) time complexity.
Here's how it's thought.
Idea: Now the numbers are 1-n. The prime number is no more than 1 and itself no other approximate. So there are not a number of primes.
We traverse from 2 onwards, which is a multiple of 2 is not a prime number. So we crossed them off.
And now it's 3 from 2, because 3 is not divisible by 3, so 3 is prime, and then the multiples of 3 are not primes. We then rowed off.
And then it's 5. Since 4 is a multiple of 2 is not a prime number (this may be asked, that's a multiple of 4.) Since multiples of 4 must be multiples of 2. So it was already crossed out). So let's take a look at 5 and cross out the multiples of 5.
Let's just do this one time. N (sqrt (n)) can be obtained by the square root. Because why to sqrt (n), give a sample bar if sqrt (n) = 7. Now to 7 7 of twice times 10 has been eliminated by the preceding 2, 7 3 times times 21 has been eliminated by 3.
。。。
So by 7, only 7 of the 7 times times is still there.
Paste the following code:
#include <iostream>using namespace Std;int main () {bool B[10000];memset (b,true,sizeof (b)); for (int i=2;i< 10000;i++) {if (B[i]) {for (int j=i+i;j<10000;j+=i) {b[j]=false;}}} for (int i=1;i<10000;i++) {if (B[i]) cout<<i<< "";} System ("Pause");}
Code is the meaning of the main understanding of any write.
All right!
Thank you for sticking with it.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Calculation of prime number-Eriksson sieve method (Spacer Gold weapon)