Open In App

Check if a number can be expressed as sum of two Prime Numbers

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number n, the task is to check if it is possible to express n as the sum of two prime numbers, a and b. If such pair does not exist, return [-1, -1].

Note: If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= d, and a < c then  [a, b] is considered as our answer.

Examples: 

Input: n = 19
Output: Yes
Explanation: The number 19 can be written as 17 + 2, here 17 and 2 are both primes.

Input: n = 14
Output: Yes
Explanation: The number 14 can be written as 7 + 7.

Input: n = 11
Output: No

[Naive Approach] Checking Each Pair - O(n * sqrt(n)) time and O(1) space

The idea is to iterate through all possible pairs of numbers that can sum up to n, and for each pair, check if both numbers are prime. We start from the smallest possible prime number (2) and check if both the current number and its complement (n minus the current number) are prime.

C++
// C++ program to Check if a prime number can
// be expressed as sum of two Prime Numbers
#include <bits/stdc++.h>
using namespace std;

// Function to check if a number is prime
bool isPrime(int n) {
    if (n <= 1) return false;
    
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return false;
    }
    return true;
}

vector<int> getPrimes(int n) {
    
    // Check each possible pair
    for (int i = 2; i <= n/2; i++) {
        if (isPrime(i) && isPrime(n - i)) {
            return {i, n - i};
        }
    }
    
    // If no pair found
    return {-1, -1};
}

int main() {
    int n = 19;
    vector<int> res = getPrimes(n);
    
    cout << res[0] << " " << res[1] << endl;
    return 0;
}
Java
// Java program to Check if a prime number can
// be expressed as sum of two Prime Numbers
import java.util.ArrayList;

class GfG {

    // Function to check if a number is prime
    static boolean isPrime(int n) {
        if (n <= 1) return false;
        
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    static ArrayList<Integer> getPrimes(int n) {
        
        // Check each possible pair
        for (int i = 2; i <= n/2; i++) {
            if (isPrime(i) && isPrime(n - i)) {
                ArrayList<Integer> res = new ArrayList<>();
                res.add(i);
                res.add(n - i);
                return res;
            }
        }
        
        // If no pair found
        ArrayList<Integer> res = new ArrayList<>();
        res.add(-1);
        res.add(-1);
        return res;
    }

    public static void main(String[] args) {
        int n = 19;
        ArrayList<Integer> res = getPrimes(n);
        System.out.println(res.get(0) + " " + res.get(1));
    }
}
Python
# Python program to Check if a prime number can
# be expressed as sum of two Prime Numbers

# Function to check if a number is prime
def isPrime(n):
    if n <= 1:
        return False
    
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

def getPrimes(n):
    
    # Check each possible pair
    for i in range(2, n // 2 + 1):
        if isPrime(i) and isPrime(n - i):
            return [i, n - i]
    
    # If no pair found
    return [-1, -1]

if __name__ == "__main__":
    n = 19
    res = getPrimes(n)
    print(res[0], res[1])
C#
// C# program to Check if a prime number can
// be expressed as sum of two Prime Numbers
using System;
using System.Collections.Generic;

class GfG {

    // Function to check if a number is prime
    static bool isPrime(int n) {
        if (n <= 1) return false;
        
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    static List<int> getPrimes(int n) {
        
        // Check each possible pair
        for (int i = 2; i <= n/2; i++) {
            if (isPrime(i) && isPrime(n - i)) {
                return new List<int> { i, n - i };
            }
        }
        
        // If no pair found
        return new List<int> { -1, -1 };
    }

    static void Main(string[] args) {
        int n = 19;
        List<int> res = getPrimes(n);
        Console.WriteLine(res[0] + " " + res[1]);
    }
}
JavaScript
// JavaScript program to Check if a prime number can
// be expressed as sum of two Prime Numbers

// Function to check if a number is prime
function isPrime(n) {
    if (n <= 1) return false;

    for (let i = 2; i * i <= n; i++) {
        if (n % i === 0)
            return false;
    }
    return true;
}

function getPrimes(n) {

    // Check each possible pair
    for (let i = 2; i <= Math.floor(n / 2); i++) {
        if (isPrime(i) && isPrime(n - i)) {
            return [i, n - i];
        }
    }

    // If no pair found
    return [-1, -1];
}

let n = 19;
let res = getPrimes(n);
console.log(res[0], res[1]);

Output
2 17

[Expected Approach] Using Sieve of Eratosthenes - O(n * log(log n)) time and O(n) space

The idea is to precompute all prime numbers up to n using the Sieve of Eratosthenes algorithm. Then, we can quickly check if any two prime numbers from our precomputed list sum up to n.

C++
// C++ program to Check if a prime number can
// be expressed as sum of two Prime Numbers
#include <bits/stdc++.h>
using namespace std;

vector<int> getPrimes(int n) {
    
    vector<bool> isPrime(n + 1, true);
    isPrime[0] = isPrime[1] = false; 
    
    // Apply Sieve of Eratosthenes
    for (int p = 2; p * p <= n; p++) {
        
        // If isPrime[p] is true, then it is a prime
        if (isPrime[p]) {
            
            // Update all multiples of p
            for (int i = p * p; i <= n; i += p)
                isPrime[i] = false;
        }
    }
    
    // Check for each possible pair of primes
    for (int i = 2; i <= n/2; i++) {
        if (isPrime[i] && isPrime[n - i]) {
            return {i, n - i};
        }
    }
    
    // If no pair found
    return {-1, -1};
}

int main() {
    int n = 19;
    vector<int> res = getPrimes(n);
    
    cout << res[0] << " " << res[1] << endl;
    return 0;
}
Java
// Java program to Check if a prime number can
// be expressed as sum of two Prime Numbers
import java.util.ArrayList;

class GfG {

    static ArrayList<Integer> getPrimes(int n) {

        boolean[] isPrime = new boolean[n + 1];
        for (int i = 0; i <= n; i++)
            isPrime[i] = true;
        isPrime[0] = isPrime[1] = false;

        // Apply Sieve of Eratosthenes
        for (int p = 2; p * p <= n; p++) {

            // If isPrime[p] is true, then it is a prime
            if (isPrime[p]) {

                // Update all multiples of p
                for (int i = p * p; i <= n; i += p)
                    isPrime[i] = false;
            }
        }

        // Check for each possible pair of primes
        for (int i = 2; i <= n / 2; i++) {
            if (isPrime[i] && isPrime[n - i]) {
                ArrayList<Integer> res = new ArrayList<>();
                res.add(i);
                res.add(n - i);
                return res;
            }
        }

        // If no pair found
        ArrayList<Integer> res = new ArrayList<>();
        res.add(-1);
        res.add(-1);
        return res;
    }

    public static void main(String[] args) {
        int n = 19;
        ArrayList<Integer> res = getPrimes(n);
        System.out.println(res.get(0) + " " + res.get(1));
    }
}
Python
# Python program to Check if a prime number can
# be expressed as sum of two Prime Numbers

def getPrimes(n):
    isPrime = [True] * (n + 1)
    isPrime[0] = isPrime[1] = False

    # Apply Sieve of Eratosthenes
    for p in range(2, int(n ** 0.5) + 1):

        # If isPrime[p] is true, then it is a prime
        if isPrime[p]:

            # Update all multiples of p
            for i in range(p * p, n + 1, p):
                isPrime[i] = False

    # Check for each possible pair of primes
    for i in range(2, n // 2 + 1):
        if isPrime[i] and isPrime[n - i]:
            return [i, n - i]

    # If no pair found
    return [-1, -1]

if __name__ == "__main__":
    n = 19
    res = getPrimes(n)
    print(res[0], res[1])
C#
// C# program to Check if a prime number can
// be expressed as sum of two Prime Numbers
using System;
using System.Collections.Generic;

class GfG {

    static List<int> getPrimes(int n) {

        bool[] isPrime = new bool[n + 1];
        for (int i = 0; i <= n; i++)
            isPrime[i] = true;
        isPrime[0] = isPrime[1] = false;

        // Apply Sieve of Eratosthenes
        for (int p = 2; p * p <= n; p++) {

            // If isPrime[p] is true, then it is a prime
            if (isPrime[p]) {

                // Update all multiples of p
                for (int i = p * p; i <= n; i += p)
                    isPrime[i] = false;
            }
        }

        // Check for each possible pair of primes
        for (int i = 2; i <= n / 2; i++) {
            if (isPrime[i] && isPrime[n - i]) {
                return new List<int> { i, n - i };
            }
        }

        // If no pair found
        return new List<int> { -1, -1 };
    }

    static void Main(string[] args) {
        int n = 19;
        List<int> res = getPrimes(n);
        Console.WriteLine(res[0] + " " + res[1]);
    }
}
JavaScript
// JavaScript program to Check if a prime number can
// be expressed as sum of two Prime Numbers

function getPrimes(n) {
    let isPrime = new Array(n + 1).fill(true);
    isPrime[0] = isPrime[1] = false;

    // Apply Sieve of Eratosthenes
    for (let p = 2; p * p <= n; p++) {

        // If isPrime[p] is true, then it is a prime
        if (isPrime[p]) {

            // Update all multiples of p
            for (let i = p * p; i <= n; i += p)
                isPrime[i] = false;
        }
    }

    // Check for each possible pair of primes
    for (let i = 2; i <= Math.floor(n / 2); i++) {
        if (isPrime[i] && isPrime[n - i]) {
            return [i, n - i];
        }
    }

    // If no pair found
    return [-1, -1];
}

let n = 19;
let res = getPrimes(n);
console.log(res[0] + " " + res[1]);

Output
2 17

Article Tags :
Practice Tags :

Similar Reads