This code is ingenious, and the personal understanding is written in the comments.
#include <stdio.h>#include<stdlib.h>#include<math.h>//Related papers: [1] Zhang Jinglong, Huang, Wang Aisong, etc. improvement of prime number determination algorithm [J]. Journal of Henan Institute of Science and Technology (natural Science Edition), (6): 61-64.doi:10.3969/j.issn.1008-7516.2013.06.015.//the number of primes within the output of 100, ideas://method of Judging primes 1://if the natural number N is not a prime, then except 1 and itself, there must be at least two numbers a and B, making a*b=n,//then A and B must have one greater than or equal to sqrt (n) and the other less than or equal to sqrt (n). Here's a rough proof//if n is composite, there must be a vegetarian factor less than or equal to the square root N. //because any composite can be expressed as a product of two or more primes. //if n is composite and its factor is greater than the square root N, then there will be a contradiction: the radical square n n>n. So composite must have (at least) a vegetarian factor that is not less than N. //n the factor <=sqrt (n) of the minor root, the n-1 factor <=sqrt (n-1), apparently sqrt (n-1) <sqrt (n);//so the factor range of the natural number within the 2~n is 2~SQRT (n); in other words, multiples of 2~SQRT (n) cover the composite in the 2~n range .//The array record, initialized to 0, is judged to be composite to 1,#defineN 100intMain () {intisprim[n+1]={0}; inti,j; //The judging condition must be I<=SQRT (n)//method of judging primes 2//where 2-SQRT (n) (rounding down) is not the prime number of the judge://According to: Judge the natural number N is not composite, is to see 2~ (N-1) between the wood can divide n, in other words is 2~ (N-1) between the number of wood is N. //because the number in "2-sqrt (n)" is composite, its factor must be in the number smaller than its own,//judged by 2 that 3 was not composite, by 2, 3 judged 4 was not composite,//judged by 2,3,4 5 is not composite, and so on ... 2~ (sqrt (n)-1) determines if sqrt (n) is composite//Combined with the above two methods of judging the number of primes, the 2-SQRT (n) (rounding down) section is based on Method 2 combined with Method 1,//(sqrt (n) (rounded down) +1) ~n using Method 1 for(i=2; I<=sqrt (n); i++){ if(isprim[i]==0){ //The basis of this judgment is mainly method 2.//If this is not the case, then the multiples of 4,6,8,10 will be set to 1,//Since these multiples are already multiples of 2 and have been placed over 1, this has been done in a repetitive effort. for(j=2*i;j<=n;j+=i) {//The cycle of this multiple is good, learning underisprim[j]=1; } } } for(i=2; i<=n;i++){ if(isprim[i]==0) printf ("%d", i); } return 0;}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Related papers: [1] Zhang Jinglong, Huang, Wang Aisong, etc. improvement of prime number determination algorithm [J]. Journal of Henan Institute of Science and Technology (natural Science Edition), (6): 61-64.doi:10.3969/j.issn.1008-7516.2013.06.015.
The number of primes within the output of 100, ideas:
Method of judging primes 1:
If the natural number N is not a prime, then except 1 and itself, there must be at least two numbers a and B, making a*b=n,
Then A and B must have one greater than or equal to sqrt (n) and the other less than or equal to sqrt (n). Here's a rough proof
If n is composite, there must be a vegetarian factor less than or equal to the square root N.
Because any composite can be expressed as a product of two or more primes.
If n is composite and its factor is greater than the square root N, then there will be a contradiction: the radical square n n>n. So composite must have (at least) a vegetarian factor that is not less than N.
n the factor <=sqrt (n) of the minor root, the n-1 factor <=sqrt (n-1), apparently sqrt (n-1) <sqrt (n);
So the factor range of the natural number within the 2~n is 2~SQRT (n); in other words, multiples of 2~SQRT (n) cover the composite in the 2~n range.
The array record, initialized to 0, is judged to be composite to 1,
#define N 100
int main ()
{
int isprim[n+1]={0};
int i,j;
The judging condition must be I<=SQRT (n)
Method of Judging primes 2
where 2-SQRT (n) (rounding down) is not the prime number of the judge:
According to: Judge the natural number N is not composite, is to see 2~ (N-1) between the wood can divide n, in other words is 2~ (N-1) between the number of wood is N.
Because the number in "2-sqrt (n)" is composite, its factor must be in the number smaller than its own,
Judged by 2 that 3 was not composite, by 2, 3 judged 4 was not composite,
Judged by 2,3,4 5 is not composite, and so on ... 2~ (sqrt (n)-1) determines if sqrt (n) is composite
Combined with the above two methods of judging the number of primes, the 2-SQRT (n) (rounding down) section is based on Method 2 combined with Method 1,
(sqrt (n) (rounded down) +1) ~n using Method 1
for (I=2;I<=SQRT (n); i++) {
if (isprim[i]==0) {
The basis of this judgment is mainly method 2.
If this is not the case, then the multiples of 4,6,8,10 will be set to 1,
Since these multiples are already multiples of 2 and have been placed over 1, this has been done in a repetitive effort.
for (j=2*i;j<=n;j+=i) {//This multiple loop is good, learning under
Isprim[j]=1;
}
}
}
for (i=2;i<=n;i++) {
if (isprim[i]==0)
printf ("%d", I);
}
return 0;
}
C-language output 2~100 primes