Given two numbers **A** and **B**. The task is to find the smallest positive integer greater than or equal to 1 such that the sum of all three numbers become a prime number.

**Examples:**

Input:A = 2, B = 3

Output:2

Explaination:

The third number is 2 because if we add all three number the

sum obtained is 7 which is a prime number.

Input:A = 5, B = 3

Output:3

**Approach: **

- First of all store the sum of given 2 number in
**sum**variable. - Now, check if
**bitwise and (&)**of sum and 1 is equal to 1 or not (checking if the number is odd or not). - If it is equal to 1 then assign 2 to variable
**temp**and go to step 4. - Else check sum value of sum and temp variable whether it is prime number or not. If prime, then print the value of temp variable else add 2 to temp variable untill it is less than a prime value.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that will check ` `// whether number is prime or not ` `bool` `prime(` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 2; i * i <= n; i++) { ` ` ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Function to print the 3rd number ` `void` `thirdNumber(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `sum = 0, temp = 0; ` ` ` ` ` `sum = a + b; ` ` ` `temp = 1; ` ` ` ` ` `// If the sum is odd ` ` ` `if` `(sum & 1) { ` ` ` `temp = 2; ` ` ` `} ` ` ` ` ` `// If sum is not prime ` ` ` `while` `(!prime(sum + temp)) { ` ` ` `temp += 2; ` ` ` `} ` ` ` ` ` `cout << temp; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 3, b = 5; ` ` ` ` ` `thirdNumber(a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that will check ` `// whether number is prime or not ` `static` `boolean` `prime(` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = ` `2` `; i * i <= n; i++) ` ` ` `{ ` ` ` `if` `(n % i == ` `0` `) ` ` ` `return` `false` `; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Function to print the 3rd number ` `static` `void` `thirdNumber(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `sum = ` `0` `, temp = ` `0` `; ` ` ` ` ` `sum = a + b; ` ` ` `temp = ` `1` `; ` ` ` ` ` `// If the sum is odd ` ` ` `if` `(sum == ` `0` `) ` ` ` `{ ` ` ` `temp = ` `2` `; ` ` ` `} ` ` ` ` ` `// If sum is not prime ` ` ` `while` `(!prime(sum + temp)) ` ` ` `{ ` ` ` `temp += ` `2` `; ` ` ` `} ` ` ` `System.out.print(temp); ` `} ` ` ` `// Driver code ` `static` `public` `void` `main (String []arr) ` `{ ` ` ` `int` `a = ` `3` `, b = ` `5` `; ` ` ` ` ` `thirdNumber(a, b); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function that will check ` `# whether number is prime or not ` `def` `prime(n): ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` `if` `i ` `*` `i > n ` `+` `1` `: ` ` ` `break` ` ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Function to print the 3rd number ` `def` `thirdNumber(a, b): ` ` ` `summ ` `=` `0` ` ` `temp ` `=` `0` ` ` ` ` `summ ` `=` `a ` `+` `b ` ` ` `temp ` `=` `1` ` ` ` ` `#If the summ is odd ` ` ` `if` `(summ & ` `1` `): ` ` ` `temp ` `=` `2` ` ` ` ` `#If summ is not prime ` ` ` `while` `(prime(summ ` `+` `temp) ` `=` `=` `False` `): ` ` ` `temp ` `+` `=` `2` ` ` ` ` `print` `(temp) ` ` ` `# Driver code ` `a ` `=` `3` `b ` `=` `5` ` ` `thirdNumber(a, b) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function that will check ` `// whether number is prime or not ` `static` `bool` `prime(` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 2; i * i <= n; i++) ` ` ` `{ ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Function to print the 3rd number ` `static` `void` `thirdNumber(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `sum = 0, temp = 0; ` ` ` ` ` `sum = a + b; ` ` ` `temp = 1; ` ` ` ` ` `// If the sum is odd ` ` ` `if` `(sum == 0) ` ` ` `{ ` ` ` `temp = 2; ` ` ` `} ` ` ` ` ` `// If sum is not prime ` ` ` `while` `(!prime(sum + temp)) ` ` ` `{ ` ` ` `temp += 2; ` ` ` `} ` ` ` `Console.Write (temp); ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `a = 3, b = 5; ` ` ` ` ` `thirdNumber(a, b); ` `} ` `} ` ` ` `// This code is contributed by Sachin. ` |

*chevron_right*

*filter_none*

**Output:**

3

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:

- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Insert minimum number in array so that sum of array becomes prime
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Finding sum of digits of a number until sum becomes single digit
- Find minimum value to assign all array elements so that array product becomes greater
- Find three prime numbers with given sum
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Remove characters from a numeric string such that string becomes divisible by 8
- Remove minimum elements from the array such that 2*min becomes more than max
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Add minimum number to an array so that the sum becomes even
- Count all prime numbers in a given range whose sum of digits is also prime
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Find three integers less than or equal to N such that their LCM is maximum
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find coordinates of a prime number in a Prime Spiral
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Probability of getting more value in third dice throw

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.