1 / 25
Jan 2011

#include<iostream.h>
struct node
{
	int p;
	node*next;
};
node*ptr,*start=NULL,*Rear;
void fn()
{
	if (start==NULL)
	{
		start=Rear=ptr;
	}
	else
	{
		Rear->next=ptr;
		Rear=ptr;
	}
}
void main()
{
	unsigned long int m,n,i,j,a[10][2];
	int t,flag=0,r;
	cin>>t;
	for(i=0;i<t;i++)
	{
		cin>>m>>n;
		for (r=0;r<10;r++)
		{
			a[r][0]=m;
			a[r][1]=n;
		}
		for (i=m;i<=n;i++)
		{  for (j=2;j<=m/2;j++)
			{
				if (i%j==0)
				{
					flag=1;
					break;
				}
				if (flag==1)
				break;
			}
			if (flag==0)
			ptr->p=i;
			ptr->next=NULL;
			fn();
		}
	}
	for (r=0;r<10;r++)
	{  cout<<"\n";
		cout<<endl;
		for (ptr=start;ptr->next!=NULL;ptr=ptr->next)
		{
			if (ptr->p>a[r][0]&&ptr->p<a[r][1])
			{
				cout<<ptr->p;cout<<"\n";
			}
		}
	}
}

where am i going wrong?

  • created

    Jan '11
  • last reply

    Jul '20
  • 24

    replies

  • 2.3k

    views

  • 11

    users

  • 1

    like

  • 3

    links

Your code is wrong for many reasons. What compiler are you using? Your code won't compile on most C compilers.

I use the Borland C++. Could you please tell me what are wrong and what is the reason.

please post it as soon as possible, it would be highly useful for me.

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.

27 days later

include

include

void main()
{
int n,flag,i=2,j,y;
clrscr();
printf("enter a range");
scanf("%d%d",&n,&y);
printf("PRIME NUMBERS ARE\n");
for(j=n;j<=y;j++)
{
flag=1;
for(i=2;i<=j/2;i++)
{
if(j%i==0)
{
flag++;
break;
}
else
flag=1;
}
if(flag==1)
printf("%d\n",j);
}
getch();
}

it will works on TCC compiler..it wont works on GCC since header file is not avail

That's correct. I would suggest that you install gcc and use it.

Mingw is a good one if you use windows.

Also, you can't use prompts in your code. Whatever you print to stdout will be interpreted as your solution. Only print what the problem statement tells you.

Read the faqs on the judge.

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

please help me as soon as possible as m unable to solve a single problem

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.

actually i have solved it out and it was accepted too......
the problem which i m facing is how to take inputs having spaces in between in c++........
if you can help me anyhow help me in that.......

8 years later

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();
}

11 days later

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 =)

Tried it but still it shows TLE.I should probably work on my algorithm.
Thanks for your time.

If i were you, i’d start by wondering if i really need to test ‘l’ for every number between 2 and i :wink:

29 days later

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;
}
}
}