990 | CodeChef

code 1—
#include
using namespace std;
int prime(int n)
{
if (n>4)
{
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
else if(n==2 || n==3)
return 1;
else
return 0;
}
int main() {
int t;
cin>>t;
while(t–)
{
int a,b;
cin>>a>>b;
for(int i=a;i<=b;i++)
{
if((prime(i)))
cout<<i<<endl;
}
}
// your code goes here
return 0;
}
works fine.

code 2—
#include
using namespace std;
typedef long long ll;
bool isPrime(ll n)
{
if(n==1)
return false;
else
{
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
}
int main() {
ll t;
cin>>t;
while(t–)
{
ll m,n;
cin>>m>>n;
for(ll k=m;k<=n;k++)
{
if(isPrime(k))
cout<<k<<endl;
}
}
// your code goes here
return 0;
}
giving tle .why???

Use fast IO. Also use sieve

1 Like