The task is to count the number of primes numbers below an input parameter N. I am testing this code with different input number ranging from small to large. It is very strange, I only encountered a runtime error if the input value is larger and equal than 46342. If the input value is smaller than 46342, the code runs perfectly fine without any runtime error.
int countPrimes(int n) {
if (n <= 2) return 0;
vector<bool> array(n,true);
int cnt = 0;
for (int i = 2 ; i < n ; i++)
{
for (int j = i ; i * j < n ; j++)
{
int product = i * j;
if (array[product])
{
array[product] = false;
cnt++;
}
}
}
return n - 2 - cnt;
}
If the input is bigger and equal than 46342, I will see "Runtime Error ", if the input is smaller than 46342, the code runs fine and the result is correct.
Aucun commentaire:
Enregistrer un commentaire