Calculate the prime number and Eratosthenes prime number by using eratosthenes distinct
Chapter 2 of C and pointers: 6th programming questions:
Prime number is the number that can only be divided by 1 and itself. The Eratosthenes distinct method is an effective method for calculating prime numbers. The first step of this algorithm is to write down all integers from 2 to an upper limit. In the remaining part of the algorithm, traverse the entire list and remove all integers that are not prime numbers.
The subsequent steps are as follows. Find the 1st unexcluded numbers (that is, 2) in the list, and then remove all the numbers after the list for each pair, because they can be divisible by 2, so they are not prime numbers. Then, return to the header of the list and start again. At this time, the first number of 1st that has not been removed in the list is 3. Therefore, after 3, the number of 3rd (a multiple of 3) is removed. After this step is completed, return to the beginning of the list. The next number after 3 is 4, but it is a multiple of 2 and has been removed. Therefore, it is skipped, and it is the turn of 5, remove the multiples of all 5, and so on and repeatedly. The unexcluded number in the final list is the prime number.
Compile a program to implement this algorithm and use arrays to represent the list. The value of each array element is used to indicate whether the corresponding number has been removed. The value of all elements in the group is set to TRUE at the start time. When the algorithm requires "Remove" the corresponding number, this element is set to FALSE. If your program runs on a 16-bit machine, consider whether to declare a variable as long. First, use an array containing 1000 elements. If you use character arrays and use the same space, you will find more prime numbers than using an integer array. You can use a subscript to indicate the pointer to the first and last elements of the array, but you should use the pointer to access the array element.
Except for 2, all even numbers are not prime numbers. The elements in the array only correspond to an odd number, which greatly improves the space efficiency of the program.
1/* 2 ** use Eratosthenes counts to output prime numbers 3 */4 # include <stdio. h> 5 6 void Eratosthenes (char * mark, int len); 7 8 int 9 main () 10 {11 char mark [2000]; 12 int I; 13 14/* 15 ** set all elements of the array to '1' 16 */17 for (I = 0; I <2000; ++ I) 18 * (mark + I) = '1'; 19 20 Eratosthenes (mark, 2000); 21 22/* 23 ** 0 corresponds to 2, directly output 24 */25 printf ("% d", 2); 26 int c = 1; // c is used to calculate the number of outputs, control 10 line breaks per output 27 28 for (I = 1; I <2000; ++ I) 29 {30 if (* (mark + I) = '1 ') 31 {32 printf ("% d", 2 * I + 1); // the elements in the array only correspond to an odd number, so subscript I corresponds to 2 * I + 133 c ++; 34 if (c % 10 = 0) 35 printf ("\ n"); 36 else37 printf (""); 38} 39} 40 41 return 0; 42} 43 44/* 45 ** Eratosthenes function, filtering prime number 46 */47 void 48 Eratosthenes (char * mark, int len) 49 {50 int I, j; 51 for (I = 1; I <len; ++ I) 52 {53 if (* (mark + I) = '1 ') 54 {55 for (j = I + 1; j <len; ++ j) 56 {57 // remove a multiple of 2 * I + 1, set it to '0' 58 if (2 * j + 1) % (2 * I + 1) = 0) 59 * (mark + j) = '0 '; 60} 61} 62} 63}