#include <stdio.h> #include <math.h> int main () { int a [ 101 ], i, j; for (i = 1 ; i <= 100 ; i ++ ) { a [i] = i; // Assign the initial value to the array } a [ 1 ] = 0 ; // Remove a [1] for (i = 2 ; i <sqrt ( 100 ); i ++) // If you need to find prime numbers in the range of 1-n The table only needs to be divided by the root number n (take an integer) { for (j = i + 1 ; j <= 100 ; j ++ ) if( a[i]!=0 && a[j]!=0 ) if(a[j]%a[i] == 0) { a[j] = 0; } } printf("\n"); int n; for(i=2,n=0; i<=100; i++) { if(a[i]!=0) { printf("%5d",a[i]); n++; } if (n == 10 ) { printf ( " \ n " ); n = 0 ; // Initialize after one completion } } return 0 ; }
The so-called screening method is the "Eratosene sieve method". A set of data is judged whether they are prime numbers one by one, and if a non-prime number is found, it will be dug out.
The algorithm can be expressed as;
(1) Dig out 1;
(2) Divide the next number p by the next undigged number p, and dig out multiples of p
(3) Check whether p is less than the integer part of the root number n, if it is, then return (2) to continue execution, otherwise it ends
(4) The rest are prime numbers