Count Primes, countprimes
Examination questions
Description:
Count the number of prime numbers less than a non-negative number,N
References:
How Many Primes Are There?
Sieve of Eratosthenes
Please use the following function to solve the problem:
Int countPrimes (int n ){
}
Problem solving code
int countPrimes(int n) { if (n == 0 || n == 1 || n == 2){ return 0; } if (n == 3){ return 1; } int temp = 0; bool flag = false; int arr[200] = { '\0' }; int k = 0; arr[0] = 2; for (int i = 3; i < n; i++){ for (int j = 0; j <= k; j++){ if (i%arr[j] == 0){ flag = true; break; } } if (flag == false){ if (k < 199){ k++; arr[k] = i;
i++; } temp++; } else{ flag = false; } } return temp+1;}
Basic algorithm ideas
To determine whether a number is a prime number, you only need to determine whether it can be divisible by a prime number smaller than this number. If not, it is a prime number.
Code annotation Analysis
Int countPrimes (int n) {// The test value required by the question is not negative, so it must contain special circumstances // because the result of 2 is also 0, therefore, if (n = 0 | n = 1 | n = 2) {return 0;} // to facilitate the subsequent algorithm design, 3 should be taken out separately if (n = 3) {return 1;} int temp = 0; // define a counter and use it to remember how many prime numbers bool flag = false; // flag, used to determine whether the number is a prime number int arr [200] = {'\ 0'}; // set the maximum value of the prime number to 200, we can test the 6th power of 10, int k = 0; // The number of prime numbers, that is, the subscript of arr, arr [0] = 2; // The first prime number is assigned a value of 2 for (int I = 3; I <n; I ++) {// The loop conforms to n> = 4, traversing All numbers smaller than n are detected for (int j = 0; j <= k; j ++) {// if this number can be used by arr [0] ~ The division of arr [k] indicates that it is not a prime number, and the flag is changed to true if (I % arr [j] = 0) {flag = true; break ;}} if (flag = false) {// if the flag is not true, it indicates that it is a prime number. if (k <199) {// we require only 200 arr prime numbers, exceeded items are not counted! K ++; arr [k] = I; // Add this prime number to the arr array I ++; // after a prime number is determined as a prime number, the number next to it cannot be a prime number (except for 2 and 3). Therefore, I ++ is used to reduce the number of non-negative samples.} temp ++; // quantity + 1} else {flag = false; // initialize the flag to false and return it to the Initial State for determining the next value} return temp + 1; // + 1 is due to the addition of the prime number 2, the above temp does not include 2}
In addition, the following solutions are available for reference (provided by Steven CZP ):
int countPrimes(int n) {bool* map = (bool*)malloc(n * sizeof(bool));memset(map, 0, n * sizeof(bool));for (int i = 2; i <= sqrt(n); i++){if (map[i])continue;int t = 2 * i;while (t < n){map[t] = true;t += i;}}int result = 0;for (int i = 2; i < n; i++){if (!map[i])result++;}return result;}
For details about how to solve this question, click here:
The twists and turns of solving a leetcode algorithm question and the reasons