 # POTATOES - Editorial

Author: Shalini Sah
Tester: Shiplu Hawlader and Mahbubul Hasan
Editorialist: Lalit Kundu

SIMPLE

Prime numbers

### PROBLEM:

Given x(<=1000) and y(<=1000), find the smallest z(>0) such that (x+y+z) is prime.

### EXPLANATION:

Keep increasing i (starting from 1) until (x+y+i) is not prime. i will never exceed 40 since x+y <= 2000. So we can afford to check each number one by one.
To check if a number N is prime:

``````def check_prime(N):
for i=2 to sqrt(N):
if N%i==0:
return false
return true
``````

Alternatively, we can use sieve of eratosthenes to check primes.
Naive primality testing (checking if N is divisible by any number from 2 to N-1) could also pass if precomputation is done.

### AUTHOR’S AND TESTER’S SOLUTIONS:

To be updated soon.

5 Likes

Why this code is giving wrong answer while submitting. It’s running fine in my local.?

import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

``````public static void main(String[] args) throws IOException {
int x,y,sum=0;
PrintWriter out = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder();
for (int i=0; i<t; i++){

x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
sum=x+y+1;
boolean prime=false;
while (true){
prime = Prime(sum);
if (prime){
break;
}
sum+=1;
}
int z= sum - (x+y);
sb.append(z).append('\n');

}
out.println(sb);
out.close();

}

boolean test = false;
for ( int j = 2 ; j <= Math.sqrt(primenum) ; j++ ){
if ( primenum % j == 0 ){
test = false;
break;
}
else
test = true;
}
return test;
}
``````

}

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int prime(int x)
{
int n, i, flag=0;
for(i=2;i<=sqrt(x);i++)
{
if(x%i==0)
{
flag=1;
break;
}
}
if (flag==0)
return(1);
else
return(-1);
}

int main()
{
int i,j,sum,sum1,check;
long long int t;
scanf("%d",&t);
long long int x[t],y[t];
for(i=0;i<t;i++)
scanf("%lld%lld",&x[i],&y[i]);
for(i=0;i<t;i++)
{
sum=0;
sum=sum+x[i]+y[i];
for(j=1;j<10;j++)
{
sum1=0;
sum1=sum+j;
//printf("%d",sum1);
check=prime(sum1);
//printf("%d\n",check);
if (check==1)
{
printf("%d\n",j);
break;
}
else
continue;
}
}
return 0;
}

why this code is giving wrong answer.Working fine on my system.

import java.io.IOException;

public class Main {

`````` public static void main(String[] args) throws IOException {
Boolean flag=true;

int req=0;
int g[]= new int[tc];
for (int tt = 0; tt < tc; tt++) {
int a = Integer.parseInt(data);
int sum = Integer.parseInt(data) + a;
int total=sum;
for(int i=1;i<31;i++)
{total=total+1;

for(int r=2;r<total;r++)
{

if(total%r==0)
{

flag=false;
break;
}

}

if(flag==true)

break;

flag=true;
}
req=total-sum;
g[tt]=req;
//  System.out.println(req);
total=0;
}
for(int k=0;k<g.length;k++)
{
System.out.println(g[k]);
}
``````

}
}

Why i am not gettin correct output.My code is:-
#include
using namespace std;
int main()
{
int i,n,j,z,T;
char x,y;
cin>>T; //inpu test cases
for(i=0;i<T;i++)
{
cin>>x[i]; //input x and y
cin>>y[i];
}

``````  for(i=0;i<T;i++)
{  j = 1;
n = x[i] + y[i] ;
while(j>0)
{
n = n + 1 ;
j++;
if(n%2!=0 && n%3!=0 &&  n%5!=0 &&  n%7!=0 || n==2 || n==3 || n==5 || n==7)
{
z[i] = n ;
break;
}
}
}
int first = z-(x+y),second = z-(x+y);
``````

cout<<first<<endl;
cout<<second;
return 0;
}

I also have a bit of problems with my code. I’m new to both Java and this website.

``````import java.util.Scanner;

class FindPotato {

public static void main (String[] args) {

//Get the x and y value
Scanner inputText = new Scanner(System.in);
int x = inputText.nextInt();
int y = inputText.nextInt();

findPrime(x, y);

int z = primeNumber -(x + y);
System.out.println(z);

}

}

/*
* Find closest prime number
* Given x, y from main method
*/
public static void findPrime (int x, int y) {

int combine = x + y;

// Restrict amount of attempts
for (int T = 1; T <= 10000; T++) {

int newNumber = x + y + T;
int i = 0;

//Count how many time a number can divide
int count = 0;

// Analyze if number is prime
while (i < newNumber) {

if (newNumber % ++i == 0) {

count ++;

// Move to next number
//if the num is not prime
if (count > 2) {

count = 0;
i = newNumber; //Exit the loop

}
}

if (i == newNumber && count == 2) {
return;

}

} //End of while loop

} // End of for-loop

} //End findPrime method

} //End class
``````

I think the problem is probably with my input. Can someone tell me how to fix this? Thanks in advance.

why this code is giving wrong answer.Working fine on my system.

#include<stdio.h>

int main(void)
{
int x,y,z;
int n;
int i,j;
int set=0;
int *ans;
int temp,k;

``````  scanf("%d",&n);
ans = (int*)malloc(n*sizeof(int));

for(i=0;i<n;i++)
{
scanf("%d %d",&x,&y);
temp = x + y;
for(j=1; ; j++)
{
temp = temp + 1;
//printf("%d\n",temp);
for(k=2;k<temp/2;k++)
{
if(temp%k==0)
{
set=1;
break;
}
}
if(set==1)
{set=0; continue; }
if(set==0)
{         *(ans+i) = j;
//printf("%d\n",*(ans+i));
break;
}
}
}

for(i=0;i<n;i++)
{
printf("%d\n",*(ans+i));
}

//getch();
return 0;
``````

}

I am getting NZEC error.Can anyone help me code here

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

bool isPrime(int sum)
{
int count=1;
for(int i=2;i<sum/2;i++)
{
if(sum%i==0)
return false;
}
return true;
}

int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int f, s;
cin>>f>>s;
int sum=f+s+1;
while(!isPrime(sum))
sum++;

``````cout << sum-f-s<<endl;
}
``````

}

why is it not working??? i am getting wrong answer

One of the simple solution to this problem is pre-computation.

Here is my code (in C)-

Commends are also included. (for those too lazy to copy paste the link-)

#include <stdio.h>

int main(void) {
int t,x,y,i=0,j,count=0,flag;
int arr={0}; /First 500 prime numbers should be more than enough, given the constraints.
however, we i took 700 just to be sure.
/
arr=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
#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;}

``````}
if(m>=1){
return 0;
}
else
return 1;
``````

}

in (x+y+i) i will never exceed 40 since x+y <= 2000.

could any one explain me this…?

1 Like

https://www.codechef.com/viewsolution/13385218

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.

1 Like

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,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