Prime Number Time Limit: 100 ms memory limit: 65536 k any questions? Click Here ^_^ Description
Evaluate the number of all prime numbers smaller than N.
Input
Multiple Input groups. Enter an integer n (n <1000000) and end with 0.
Output
Returns the number of all prime numbers within n.
Sample Input
100
Sample output
4
Prompt
Taking this question as an example, we need to find the prime number within n, n <= 1000000.
To save time, use the prime number sieve to mark all the prime numbers within 1000000!
Core algorithm code of this prime Screening:
After this code is run, the prime number is marked as 0, not marked as 1. Data processing is complete!
Int f [1000004]; int I, j; memset (F, 0, sizeof (f); F [1] = 1; // If the marker 1 is not a prime number, the marker 0 is a prime number I = 2; while (I <= 500000) {for (j = I * 2; j <= 1000000; J + = I) {f [J] = 1;} I ++; while (F [I] = 1) {I ++ ;}}
# Include <stdio. h> # include <string. h> int f [1000004]; int main () {int N; int I, j; memset (F, 0, sizeof (f); F [1] = 1; // flag is not I = 2; while (I <= 500000) {for (j = I * 2; j <= 1000000; j + = I) {f [J] = 1;} I ++; while (F [I] = 1) {I ++;} int K, CNT; while (scanf ("% d", & N) & n! = 0) {CNT = 0; For (k = 1; k <n; k ++) {If (F [k] = 0) CNT ++ ;} printf ("% d \ n", CNT);} return 0 ;}
I learned a new prime screening, which is different from the previous one!