The simplest method of sieve prime method is to start from 2, so the multiples of 2 are removed, and then starting from 3, the multiples of 3 are removed. According to this it is easy to write the code, the following code is the number of sieve method to get a prime number within 100 and saved to the primes[] array.
//by Morewindows (http://blog.csdn.net/MoreWindows ) Const intMAXN = -; BOOLFLAG[MAXN]; intPRIMES[MAXN/3], pi; voidgetprime_1 () {intI, J; Pi=0; memset (Flag,false,sizeof(flag)); for(i =2; i < MAXN; i++) if(!Flag[i]) {Primes[pi++] =i; for(j = i; J < maxn; J + =i) flag[j]=true; } }
It can be seen that there will be a lot of repeated visits, such as access to flag[2] and flag[5] Each visit flag[10] once. It is therefore best to think of ways to reduce this repeated access, so that each element of the flag[] array is only accessed once.
This can be considered-the simple sieve prime method is to use a multiple of a prime number must not be a prime, and the product of any one and all other primes is necessarily not prime (this is because each composite must have a minimum element factor).
In order to experiment with this idea, the number from 2 to 10 is first verified.
2,3,4,5,6,7,8,9,10 the flag is unmarked at the beginning.
First step access 2,flag[2] No tag so add 2 to the Prime number table, and then multiply the 2 with all the numbers in the Prime number table is necessarily not prime, 2*2=4 so Mark Flag[4].
2,3,4, 5,6,7,8,9,10
Step two access 3,flag[3] No tag so add 3 to the Prime number table, multiply the 3 with all the numbers in the Prime number table is necessarily not prime, 3*2=6,3*3=9 so Mark Flag[6] and flag[9].
2,3,4, 5,6, 7,8,9, 10
Step three access 4,flag[4] There is a tag so 4 does not join the Primes table, the number of 4 and all the numbers in the Prime number table is necessarily not a prime, 4*2=8,4*3=12 so Mark Flag[8].
2,3,4, 5,6, 7,8,9, 10
Fourth Step access 5,flag[5] No tag so add 5 to the Prime number table, multiply the 5 with all the numbers in the Prime number table is necessarily not prime, 5*2=10,5*3=15 so Mark Flag[10].
2,3,4, 5,6, 7,8,9,ten
Fifth Step access 6,flag[6] There is a tag so 6 does not join the Primes table, the number of 6 and the number of all numbers in the table is bound to be not prime, 6*2=12,6*3=18,6*5=30.
2,3,4, 5,6, 7,8,9,ten
1 //by Morewindows (http://blog.csdn.net/MoreWindows ) 2 Const intMAXN = -; 3 BOOLFLAG[MAXN]; 4 intPRIMES[MAXN/3], pi; 5 voidgetprime_2 ()6 { 7 intI, J; 8PI =0; 9memset (Flag,false,sizeof(flag)); Ten for(i =2; i < MAXN; i++) One { A if(!Flag[i]) -primes[pi++] =i; - for(j =0; (J < Pi) && (i * primes[j] < MAXN); J + +) theFlag[i * Primes[j]] =true; - } -}
Is this code right? Careful review of the analysis process, you can find that some of the data has been visited many times, which is certainly not the result we hope, our request is to let each composite only its minimum factor sieve once . For example, 12, its minimum factor is 2, so it should only be in the calculation of 6*2 to access, and should not be in the calculation of 4*3 to access, the same 18 should be in the calculation 9*2 only to access, and should not be in the calculation of 6*3 to access.
Find out why, and then think about how to solve it. 6*3 not 9*2 can be, because 6 is a multiple of 2, so after the calculation of 6*2 can no longer multiply 6 and 2 large primes, the result of these multiplication will inevitably lead to repeated calculations. So for any number, if it is a multiple of that prime, it can no longer be multiplied by the prime number in the prime number table, such as 9 is a multiple of 3, so after 9*3 can no longer be used to calculate 9*5. So add a line of judgment to the code:
1 //by Morewindows (http://blog.csdn.net/MoreWindows ) 2 Const intMAXN = -; 3 BOOLFLAG[MAXN]; 4 intPRIMES[MAXN/3], pi; 5 voidgetprime_2 ()6 { 7 intI, J; 8PI =0; 9memset (Flag,false,sizeof(flag)); Ten for(i =2; i < MAXN; i++) One { A if(!Flag[i]) -primes[pi++] =i; - for(j =0; (J < Pi) && (i * primes[j] < MAXN); J + +) the { -Flag[i * Primes[j]] =true; - if(i% primes[j] = =0)//this guarantees that each non-vegetarian number is only screened once. - Break; + } - } +}