C-language code Filtering for prime numbers and general search, filtering for prime numbers
Prime Number: Apart from itself, the number that cannot be divisible by other integers is called a prime number.
C code (common)
# Include
# Include
# Include
# Define N 100 // common method int main () {int I, j; for (I = 2; I <N; I ++) {for (j = 2; j <= sqrt (I); j ++) {if (I % j = 0) {break ;}} if (j> sqrt (I )) {printf ("% d \ n", I) ;}// return 0; system ("pause ");}
Limit Method
# Include
# Include
# Include
# Define N 100 // The number of queries for filtering // The specific screening method is to first sort n natural numbers in order. // 1 is not a prime number or a combination. // The second number 2 is the prime number, and all the numbers that can be divisible by 2 after 2 are excluded. // The first undivided number behind 2 is 3, leaving 3, and then dividing all the numbers behind 3 that can be divisible by 3. // 3 The first unallocated number after the end is 5, leaving 5, and then dividing all the numbers that can be divisible by 5. // As a result, all the groups not greater than N will be screened out, leaving all the prime numbers not greater than N. Int main () {int num = 0; int flag [N + 1]; int I, j; for (I = 2; I <= N; I ++) {flag [I] = 1; // initialization, indicating that there is no number division} for (I = 2; I * I <= N; I ++) {if (flag [I] = 1) {for (j = 2; I * j <= N; j ++) {flag [I * j] = 0; // calculate the multiple of I }}for (I = 0; I <= N; I ++) {if (flag [I] = 1) {printf ("% 4d", I); num ++; if (num % 10 = 0) {printf ("\ n ");}}} printf ("\ n"); system ("pause ");}