So, I made this little program. It gives all the correct outputs, but when uploading it to the online judge it throw me away a Time Limited Exceeded. Any ideas on how to make it more efficient?
Here is my code.
int const MAX = 1000001;
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
int count = 0, array[MAX], a, b, pi = 0;
bool end = false;
for(int i = 1; i <= MAX; i++)
{
count = 0;
for(int j = 2; j <= sqrt(i); j++)
{
if(i % j == 0)
{
array[i]=0;
count++;
break;
}
}
if(count == 0 && i != 1){
array[i]=1;
}
}
do {
cin >> a >> b;
pi = 0;
if ((a == 0) && (b == 0)) {
end = true;
break;
}
else {
for (int i = a; i <= b; i++) {
if (array[i] == 1) {
pi ++;
}
}
cout << pi << endl;
}
} while (end == false);
cout << endl;
return 0;
}