Thursday, October 9, 2025
HomeData Modelling & AIFind third number such that sum of all three number becomes prime

Find third number such that sum of all three number becomes prime

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:
Explanation: 
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:
 

 

Approach: 
 

  1. First of all store the sum of given 2 number in sum variable.
  2. Now, check if bitwise and (&) of sum and 1 is equal to 1 or not (checking if the number is odd or not).
  3. If it is equal to 1 then assign 2 to variable temp and go to step 4.
  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 until 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;
}


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


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


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.


Javascript




<script>
    // Javascript implementation of the above approach
     
    // Function that will check
    // whether number is prime or not
    function prime(n)
    {
        for (let i = 2; i * i <= n; i++) {
 
            if (n % i == 0)
                return false;
        }
        return true;
    }
 
    // Function to print the 3rd number
    function thirdNumber(a, b)
    {
        let sum = 0, temp = 0;
 
        sum = a + b;
        temp = 1;
 
        // If the sum is odd
        if ((sum & 1) != 0) {
            temp = 2;
        }
 
        // If sum is not prime
        while (!prime(sum + temp)) {
            temp += 2;
        }
        document.write(temp);
    }
     
    let a = 3, b = 5;
    thirdNumber(a, b);
 
// This code is contributed by divyeshrabadiya07.
</script>


Output: 

3

 

Time Complexity: O(sqrt(n))

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32345 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6714 POSTS0 COMMENTS
Nicole Veronica
11877 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11940 POSTS0 COMMENTS
Shaida Kate Naidoo
6834 POSTS0 COMMENTS
Ted Musemwa
7094 POSTS0 COMMENTS
Thapelo Manthata
6789 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS