Use the gcc compiler. If you're using windows look up mingw. This is the compiler that spoj uses. Your code does not compile when submit to spoj. The first compile error that is returned is that main must return an int.
You can use ideone in the meantime, it's an online IDE that will use re same compiler as spoj.
include
include
include
int main()
{
int temp=1,t,l,m=0,n=0,i,j=0;
char a[40];
cin>>t;
for(i=1;i<=t;i++)
{
temp=1;
gets(a);
while(a[j]!=' ')
{
l=a[j];l=l-48;
m=10*m+l;
j++;
}
j++;
while(a[j]!='\0')
{
l=a[j];l=l-48;
n=10*n+l;
j++;
}
if(m==1||m==2)
{
cout<<"2\n";
m=3;
}
if(m%2==0)
m++;
for(int k=m;k<=n;k=k+2)
{
for(int z=3;z<=sqrt(k);z=z+2)
if(k%z==0)
{
temp=0;
break;
}
if(t==1)
cout< }
cout<<"\n";
}
}
whats wrong with it..............what are other ways for solving it..............its taking much time to execute
The first problem to start with is spoj.pl/problems/TEST22
Go to the Problemset Archive forum and read the post titled "Read this befor posting"
There are multiple test cases, what if all of the test cases were exactly the same? You would be duplicating a lot of work, right? The list of prime numbers first change.
What’s the problem with this code??
It works fine on my IDE.But SPOJ is showing time limit exceeded.Please help me.
#include
using namespace std;
int main()
{
int t, a;
int m[10], n[10];
cin >> t;
cout << endl;
for (int k = 0; k < t; k++) {
cin >> m[k] >> n[k];
cout << endl;
}
for (int j = 0; j < t; j++) {
if (m[j] == 0 || m[j] == 1 || m[j] == 2)
cout << 2 << endl;
for (int i = m[j]; i <= n[j]; 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;
}
return 0;
}
What’s the problem with this code??
It works fine on my IDE.But SPOJ is showing time limit exceeded.Please help me.
#include
using namespace std;
int main() {
int t,a;
int m[10],n[10];
cin>>t;
cout<<endl;
for(int k=0;k<t;k++)
{
cin>>m[k]>>n[k];
cout<<endl;
}
for(int j=0;j<t;j++)
{
if(m[j]==0||m[j]==1||m[j]==2)
cout<<2<<endl;
for(int i=m[j];i<=n[j];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;
}
return 0;
}
This code works fine on your IDE ?
I suggest trying to compile with g++ then.
You start with #include but don’t specify anything to include.
I guess you want to include considering you’re using cout and cin.
Then, you’re outputing a newline everytime you read something. Why ?
Considering the TLE, that just means you’ll have to rework your algorithm to make it faster.
I’m not going to dive into the algorithm details, but you might want to avoid the use of cin and cout, which are slow.
Also, you don’t have to store the whole input, just process each line one by one :
for (int i = 0; i < nb_test_cases; ++i)
{
read_input_line();
process_input_line();
print_ouput();
}
Thankyou and sorry for the late reply.Removed arrays for storing values and endl. How to avoid the use of cin and cout.I am new to programming.can you give any resources?
Here is my modified code.
#include iostream //with angular brackets don’t know why spoj prevents it from printing
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;
}
}
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 =)
What’s the problem with this code?
this is working well on ideone.com but in spoj commpilation error is coming.
#include
using namespace std;
void prime(int,int);
int main() {
int a,m,n,b,c;
cin>>a; //test case
// cout<<endl;
cin>>m; // cout<<" “;
cin>>n; // cout<<endl;
cin>>b; // cout<<” ";
cin>>c; if(1 <= m<=n && n <= 1000000000 && n-m<=100000){
prime(m,n); cout<<endl;
prime(b,c);
}
return 0;
}
void prime(int x,int y){
int i=2,a;
for(a=x;a<=y;a++){
if(a==2) cout<<endl<<a;
for(i=2;i<a;i++){
if(a%i==0) break;
if(a%i != 0 && i== a-1) cout<<endl<< a;
}
}
}