Add the code flag to tell the forum youâre trying to write code:
[code] at the begining and [/code] at the end.
Hereâs a formated version for better readbility:
#include <iostream>
using namespace std;
void main()
{
int t,a,m,n;
cin >> t;
for (int k = 0; k < t; k++)
{
cin >> m >> n;
if (m == 0 || m == 1 || m == 2)
cout << 2 << endl;
for (int i = m; i <= n; i++)
{
for (int l = 2; l < i; l++)
{
if (i % l == 0)
{
a = 0;
break;
}
else
a = 1;
}
if (a == 1)
cout << i << endl;
}
cout << endl;
}
}
If youâre gonna use cin and cout, use
ios_base::sync_with_stdio(false);
It disables the syncing of different stream methods, which slows down the stream processing and isnât necessary in this kind of program.
Then i suggest you also replace your âendlâ by a simple newline: â\nâ: endl forces a flush of the stream each time it is called, which is also unnecessary in this program.
Just using this should improve the speed, and might get you under the time limit.
If it doesnât, then youâll have to rethink your algorithm a bit, maybe try to improve the boundaries of your loops, figure out better ways to do certain thingsâŚ
good luck =)