I was solving a question in Hackerrank and the question was to find the prime number count in a range. Since using the normal methodology was facing timeout, I used Sieve of Eratosthenes. Most testcases worked except two hidden testcases. I ran the code in a GDB compiler and figured out that the code only supports values upto 6 million. What do I do? The code is given below:
#include<cstring>
#include<cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
void SieveOfEratosthenes(unsigned long long int a,unsigned long long int b)
{
unsigned long long int count=0;
bool prime[b+1];
memset(prime, true, sizeof(prime));
for (unsigned long long int p=2; p*p<=b; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
for (unsigned long long int i=p*p; i<=b; i += p)
prime[i] = false;
}
}
for (unsigned long long int p=a; p<b; p++)
if (prime[p] &&p!=1)
count++;
cout<<count;
}
int main()
{
unsigned long long int a,b;
cin>>a>>b;
SieveOfEratosthenes(a,b);
return 0;
}