POTATOES - Editorial

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;

}

can anyone please help me to search out why I am getting wrong answer after submitting the code?

#include
#include<bits/stdc++.h>
using namespace std;

vectorarr(2000);
int n=2000;
void sieve(int n){
bool prime[2001];
memset(prime,true,sizeof(prime));
for(int i=2;ii<=n;i++){
if(prime[i]==true)
for(int j=i
i;j<=n;j+=i){
prime[j]=false;
}
}
for(int i=2;i<=n;i++){
if(prime[i]){
arr.push_back(i);
}
}
}

int main() {
long long t;
cin>>t;
sieve(n);

while(t--){
    long long a,b;
    cin>>a>>b;
  
    auto it=upper_bound(arr.begin(),arr.end(),a+b);
    cout<<*it-(a+b)<<endl;
}
// your code goes here
return 0;

}

java: Sieve of Eratosthenes solution
https://www.codechef.com/viewsolution/56197489

improve:
https://www.codechef.com/viewsolution/56197789