int main(void) {
// your code goes here
int t,x,y,i=0,j,count=0,flag;
int arr[700]={0}; /First 500 prime numbers should be more than enough, given the constraints.
however, we i took 700 just to be sure./
arr[0]=2;
count=1;//count is number of prime numbers in array.
for(i=3;i<7000 && count<700;i+=2)//precomputing prime numbers till 7000.
{
for(j=2;j<i;j++)
{
flag=0;
if(i%j==0)
{
flag=1;
break;
}
}
if (flag==0)
{
arr[count]=i;
count++;
}
}
scanf ("%d",&t); //inputting T
for(i=0;i<t;i++)
{
scanf ("%d %d",&x,&y);
int sum=x+y;
for (j=0; x+y>=arr[j] && j<700;j++); /*gives the smallest prime number greater than x+y.
This prime number is stored in arr[j]. Please take a moment to note the significance of '=' sign
in x+y>=arr[j]*/
int z= arr[j]-sum;
printf ("%d\n",z);
}
return 0;
Can anyone what wrong in this code
I m getting wrong answer
#include<stdio.h>
#include<math.h>
int prime(int a );
int main(){
long int n ,i ,t,x,y,z,q,g;
scanf("%ld" , &t);
while(t–){
n=0;
scanf("%ld%ld" , &x , &y);
q=x+y;
if(q==0){
printf(“not possible”);
}else
{
for(i=1; i<=41; i++){
z=q+i;
g=prime(z);
if(g){
n=i;
break;
}
}
z=0;
printf("%ld" , n);}
}
}
int prime(int a ){
int i ,m=0 ;
for(i=2; i<=sqrt(a); i++){
if(a%i==0){
m=m+1;}
can anyone tell me whats wrong in the above code ?
I am geeting wrong answer even though I’ve checked every thing (i think),I’ve applied fermat’s little theorem to check if the number is prime or not.
just find sum of given two integers as first and second harvest and find just next prime number , then answer will be that prime number - sum(sum of first and second harvest).
Your program fails to find 2 and 3 as prime numbers. If x=1 and y=1 your program outputs 3, but the correct answer is 1. You can correct it by making value of test as true before entering loop and no need of else part within the loop.
Your program fails for the input x=1326, y=1, the correct answer is 34(since 1361 is the next prime after 1327). Your program finds the answers only up to 30, make the condition of the loop in i as i<35
The potatoes in the last farm will never exceed 40 as the sum of the potatoes in the previous two farms will always be less than 2000. And there lies a prime number in every 40 numbers before 2000.