POTATOES - Editorial

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.

1 Like

there were accepted answers that took more time than this… but this showed a TLE. please help.

thanks jawad, it solved the problem

why am i gettin wrong answer for this code?

I’ve tried most of the test cases with this code, it works fine in my local.

#include<stdio.h>
void main()
{
int t,a,b,c[1000],i=0,t1;
scanf("%d",&t);
t1=t;
while(t>0) // t test cases
{
int ct=0,p=0,j; //3 feild count
scanf("%d",&a);
scanf("%d",&b);
a+=b;
while(p!=2)
{
p=0;
ct++;
a++;
for( j=2;j<(a/2);j++)
{
if(a%j==0)
{
p=1;
}
}
if(p!=1)
{
c[i]=ct;
p=2;
i++;
}
}
t–;
}
for( i=0;i<t1;i++)
{
printf("%d\n",c[i]);
}
}

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

thanks jawad

http://www.codechef.com/viewsolution/3947463

Please check out this solution. Works perfectly fine for all the test cases, still I’m getting a wrong answer!

just print new line after each output. i.e:
printf("%d\n",A[i]-(x+y));

1 Like

@achaitanyasai Thank you so much! It did run now!
It would be a great help if you could check out my question on BNGAME as well.
Here’s the link to it-
http://discuss.codechef.com/questions/43413/bngame-problem
Thanks a lot! :slight_smile:

1 Like

I din’t understand the point that “i never exceeds 40” can explain it again?

May be it means that they are no two adjacent prime numbers till 2000 that differ by more than 40, for instance 2003 is the nearest prime to 2000.

1 Like

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.

1 Like

thanks @anon62928982 and @shardic

1 Like

Hey! How did you come with the fact that i will never exceed 40?
Is there any mathematics behind that?

There’s no proof. The gap between primes is still an open topic in math. We just have the data

Can anyone check the testcases where this code might fail as I did try with random numbers and it gives the right answer.
https://www.codechef.com/viewsolution/30940468

When are you going to upload the Authors and testers solutions ???

Did you get the answer to this ? If yes , then make efforts to explain …

The problem with this one?

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class chef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t–>0) {
int x=sc.nextInt();
int y=sc.nextInt();
int sum=x+y;
int c=0;
for(int i=1;i<=1000;i++) {
sum=sum+i;
for(int j=1;j<=sum;j++) {
if(sum%j==0)
c++;
}
if(c==2)
System.out.println(i);
break;
}
}
}
catch(Exception e) {
}
}

}

#include<bits/stdc++.h>

#define ll long long

#define testCases int t; cin >> t; while(t–)

using namespace std;

void solution();

int nextPrime(int prime);

int isPrime(int m);

int main(){

ios_base::sync_with_stdio(false);

cin.tie(NULL);

solution();



return 0;

}

void solution(){

testCases{

        int x, y;

        cin >> x >> y;

        cout << nextPrime(x+y) - (x + y )<< endl;

}

}

int nextPrime(int prime){

if (prime < 2)

    return 2;

bool flag = 0;

while(!flag){

    flag = isPrime(++prime);

}

return prime;

}

int isPrime(int m){

//base case

if(m < 2)

    return 0;

if(m < 4)

    return 1;

if(m % 2 == 0 || m % 3 == 0)

    return 0;

for(int i = 5; i*i <= m; i+=6){

    if((m % i == 0) || (m % (i+2) == 0)) return 0;

}

return 1;

}