5 / 5
Jul 2008

Can anyone help me with the reading in the PRIME1 problem. I use C.
Thanks in advance.

Well I programmed this

#include<stdio.h>
#include<stdlib.h>
int main(){
	 int initial;
	 int first;
	 int sec;
	 int i,j;
	scanf("%d",&initial);
    for(i=0;i<initial;i++){
	
	scanf("%d",&first);
	scanf("%d",&sec);
	for(j=first;j<=sec;j++){
		if(j==1)
			continue;
		if((j==2)||(j==3)||(j==5)||(j==7))
			printf("%d \n",j);
		if((j%2!=0)&&(j%3!=0)&&(j%5!=0)&&(j%7!=0))
			printf("%d \n",j);
						
	}
}
return 0;
}

It is based on the sieve of Eratosthenes, it works well for several testaceses i tried, but the judge says that the answer is wrong. This happens because the program is wrong or because of the input-output method I use.

Well I programmed this

#include<stdio.h>
#include<stdlib.h>
int main(){
	 int initial;
	 int first;
	 int sec;
	 int i,j;
	scanf("%d",&initial);
    for(i=0;i<initial;i++){
	
	scanf("%d",&first);
	scanf("%d",&sec);
	for(j=first;j<=sec;j++){
		if(j==1)
			continue;
		if((j==2)||(j==3)||(j==5)||(j==7))
			printf("%d \n",j);
		if((j%2!=0)&&(j%3!=0)&&(j%5!=0)&&(j%7!=0))
			printf("%d \n",j);
						
	}
}
return 0;
}

It is based on the sieve of Eratosthenes, it works well for several testaceses i tried, but the judge says that the answer is wrong. This happens because the program is wrong or because of the input-output method I use.

So.. you test to see if the number is divisible by 2, 3, 5, or 7, and if not you call it a prime? What about if its divisible by 11, 13, 17, 19, 23...

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 34 26d

Want to read more? Browse other topics in C and C++ or view latest topics.