Given two integers **X** and **Y** where **X > Y**, the task is to check if there exists an prime number **P** such that if **P** is repeatedly subtracted from **X** then it gives **Y**.

**Examples:**

Input:X = 100, Y = 98

Output:Yes

(100 – (2 * 1) = 98)

Input:X = 45, Y = 31

Output:Yes

(45 – (7 * 2)) = 31

**Naive approach:** Run a loop for every integer starting from **2** to **x**. If the current number is a prime number and it meets the criteria given in the question, then it is the required number.

**Efficient approach:** Notice that for a valid prime **p**, **x – k * p = y** or **x – y = k * p**. Suppose, **p = 2** then **(x – y) = 2, 4, 6, …** (all even numbers). This means if **(x – y)** is even then the answer is always true. If **(x – y)** is an odd number other than **1**, it will always have a prime factor. Either it itself is a prime or it is a product of a smaller prime and some other integers. So the answer is True for all odd numbers other than **1**.

What if **(x – y) = 1**, it is neither a prime nor composite. So this is the only case where the answer is false.

Below is the implementation of the approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if any ` `// prime number satisfies ` `// the given conditions ` `bool` `isPossible(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// No such prime exists ` ` ` `if` `((x - y) == 1) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 100, y = 98; ` ` ` ` ` `if` `(isPossible(x, y)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns true if any ` `// prime number satisfies ` `// the given conditions ` `static` `boolean` `isPossible(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// No such prime exists ` ` ` `if` `((x - y) == ` `1` `) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `x = ` `100` `, y = ` `98` `; ` ` ` ` ` `if` `(isPossible(x, y)) ` ` ` `System.out.print(` `"Yes"` `); ` ` ` `else` ` ` `System.out.print(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function that returns true if any ` `# prime number satisfies ` `# the given conditions ` `def` `isPossible(x, y): ` ` ` ` ` `# No such prime exists ` ` ` `if` `((x ` `-` `y) ` `=` `=` `1` `): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Driver code ` `x ` `=` `100` `y ` `=` `98` ` ` `if` `(isPossible(x, y)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that returns true if any ` `// prime number satisfies ` `// the given conditions ` `static` `bool` `isPossible(` `int` `x, ` `int` `y) ` `{ ` ` ` ` ` `// No such prime exists ` ` ` `if` `((x - y) == 1) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `x = 100, y = 98; ` ` ` ` ` `if` `(isPossible(x, y)) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if there exists a number with X factors out of which exactly K are prime
- Minimum number of moves after which there exists a 3X3 coloured square
- Number of times the largest perfect square number can be subtracted from N
- Smallest number to be subtracted to convert given number to a palindrome
- Number of times the largest Perfect Cube can be subtracted from N
- Least number to be added to or subtracted from N to make it a Perfect Square
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Maximum sum after repeatedly dividing N by a divisor
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Minimize Array length by repeatedly replacing co-prime pairs with 1
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- All pairs whose xor gives unique prime
- Smallest subarray which upon repetition gives the original array
- Value to be subtracted from array elements to make sum of all elements equals K
- Check whether an Array can be made 0 by splitting and merging repeatedly
- Find if there exists multiple ways to draw line through (x, y) to cut rectangle in equal halfs
- Largest number in given Array formed by repeatedly combining two same elements
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Array value by repeatedly replacing max 2 elements with their absolute difference

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.