Check if a number can be expressed as sum of two Prime Numbers
Last Updated :
11 Jul, 2025
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]);
[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]);
Similar Reads
Check if a number can be expressed as sum two abundant numbers Given a number N. The task is to express N as the sum of two Abundant Numbers. If it is not possible, print -1. Examples:Input : N = 24 Output : 12, 12Input : N = 5Output : -1 Approach: An efficient approach is to store all abundant numbers in a set. And for a given number, N runs a loop from 1 to n
13 min read
Check if an integer can be expressed as a sum of two semi-primes Given a positive integer N, check if it can be expressed as a sum of two semi-primes or not.Semi-primes A number is said to be a semi-prime if it can be expressed as product of two primes number ( not necessarily distinct ).Semi-primes in the range 1 -100 are: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33
8 min read
Check if an integer can be expressed as a sum of two semi-primes Given a positive integer N, check if it can be expressed as a sum of two semi-primes or not.Semi-primes A number is said to be a semi-prime if it can be expressed as product of two primes number ( not necessarily distinct ).Semi-primes in the range 1 -100 are: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33
8 min read
Check if a number can be written as a sum of 'k' prime numbers Given two numbers N and K. We need to find out if 'N' can be written as sum of 'K' prime numbers. Given N <= 10^9 Examples : Input : N = 10 K = 2 Output : Yes 10 can be written as 5 + 5 Input : N = 2 K = 2 Output : NoRecommended PracticePrimes sumTry It! The idea is to use Goldbach's conjecture w
8 min read
Check if a number can be expressed as product of a prime and a composite number Given a number N, the task is to check if N can be represented as the product of a prime and a composite number or not. If it can, then print Yes, otherwise No. Examples: Input: N = 52 Output: YesExplanation: 52 can be represented as the multiplication of 4 and 13, where 4 is a composite and 13 is a
12 min read
Count prime numbers that can be expressed as sum of consecutive prime numbers Given an integer N, the task is to find the number of prime numbers up to N that can be expressed as a sum of consecutive primes. Examples: Input: N = 45Output: 3Explanation:Below are the prime numbers up to 45 that can be expressed as sum of consecutive prime numbers: 5 = 2 + 317 = 2 + 3 + 5 + 741
8 min read