1

This question originates from codechef, the online contest website. It requires calculating prime numbers. The question is:

Farmer Feb has three fields with potatoes planted in them. He harvested x potatoes from the first field, y potatoes from the second field and is yet to harvest potatoes from the third field. Feb is very superstitious and believes that if the sum of potatoes he harvests from the three fields is a prime number , he'll make a huge profit. Please help him by calculating for him the minimum number of potatoes that if harvested from the third field will make the sum of potatoes prime. At least one potato should be harvested from the third field.

Input
The first line of the input contains an integer T denoting the number of test cases. Each of the next T lines contain 2 integers separated by single space: x and y.

Output
For each test case, output a single line containing the answer.

Constraints

1 ≤ T ≤ 1000
1 ≤ x ≤ 1000
1 ≤ y ≤ 1000

Example

Input:

2
1 3
4 3

Output:

1
4

Explanation

In example case 1: the farmer harvested a potato from the first field and 3 potatoes from the second field. The sum is 4. If he is able to harvest a potato from the third field, that will make the sum 5, which is prime. Hence the answer is 1 (he needs one more potato to make the sum of harvested potatoes prime).

I solved it this way:

import java.io.*;
import java.util.StringTokenizer;

class java2s {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T=Integer.parseInt(br.readLine());
        while(T-->0) {
            StringTokenizer st=new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            int sum = x + y;
            for(int i=1; i<100; i++) {
                int res=sum+i;
                if(res==3 || res==5 || res==7) {
                    System.out.println(i);
                    break;
                }
                else if((res%2)!=0 && (res%3)!=0 && (res%5)!=0 && (res%7)!=0) {
                    System.out.println(i);
                    break;
                }
            }
        }
    }
} 

When I ran that code on my PC, it worked very well, but when I submitted it to the contest website, the grader indicated that my answer was incorrect.

Can you tell me which corner cases I am missing?

shoover
  • 3,071
  • 1
  • 29
  • 40
bhavya joshi
  • 1,096
  • 10
  • 20

1 Answers1

2

First and foremost, the way input is displayed is little misleading. I guess input should look like

input:
2
1 3
4 3

and the output should look like
1
4

The code you've submitted is checking very minimal number of cases.

Answering the below questions should help you elucidating the problem

  1. Why are you only comparing the variable result to 3,5,7 where the result can also be 101 which is a prime number.

  2. To check whether or not result is a prime, Why are you dividing the result only by 2,3,5,7? example:169 is not a prime and is not divisible by any of 2,3,5,7.

  3. What is the maximum number of potatoes(in all the three fields) feb can harvest?

  4. How to check whether a number is prime or not.