1 / 4
May 2020

#include
using namespace std;
int main()
{

long long int m;
long long int n;

int t;
cout<<“enter the number of test cases”<<endl;
cin>>t;

int f;
int i;
int s;
int j;

while(t>0)
{
cout<<"\n enter the values of two integers where m is the lower bound "<<endl;
cin>>m>>n;

for(i=m;i<=n;i++)
{
    s=i;
	f=0;
	for(j=1;j<=s;j++)
	{
		if(s%j==0)
		{
			f++;
		}
		}	
	if(f==2)
	{

	cout<<s<<endl;	
}

}

t–;
}

return(0);
}
i am getting time limit exceeded in the solution ,can anyone help me please

  • created

    May '20
  • last reply

    May '20
  • 3

    replies

  • 596

    views

  • 2

    users

How many divisions do you need to do to prove that 1000000000 is not prime? How many does the code do?

Similarly, how many divisions do you need to do to prove 573,259,391 is prime? How many does the code do?

i got your point ,
i tries another way ,but it is still not working well

#include
using namespace std;

bool prime(long long int n)
{
int i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
return(false);
}
return(true);
}

int main()
{
int t=0;
long long int m=0;
long long int n=0;
int i;
cin>>t;
while(t>0)
{

cout<<"enter the values of m and n"<<endl;
cin>>m>>n;
for(i=m;i<=n;i++)
{
    if(prime(i))
	{
	
	
		cout<<i<<endl;

}
}
cout<<endl;
t–;
}

return(0);
}

Don’t write anything except the output defined in the problem.

Much better, but there’s still room for improvement. How many even numbers do you need to test?