Back to C++
2026-04-176 min read

Check Whether a Number can be Express as Sum of Two Prime Numbers (C++)

Learn Check Whether a Number can be Express as Sum of Two Prime Numbers (C++) step by step with clear examples and exercises.

Why This Matters

Understanding how to check whether a number can be expressed as the sum of two prime numbers is an essential skill for solving complex programming problems and competitive coding challenges. This ability demonstrates your proficiency in manipulating numbers effectively in C++ and understanding various algorithms for prime number-related problems.

By mastering this concept, you'll be better prepared to tackle more intricate mathematical and computational tasks that require efficient handling of prime numbers.

Prerequisites

To follow this lesson, you should have a good understanding of:

  1. Basic C++ syntax and control structures (if-else statements, loops)
  2. Understanding what prime numbers are and how to check if a number is prime or not in C++
  3. Familiarity with functions, arrays, input/output operations, and standard libraries like ` and ` in C++
  4. Knowledge of data structures such as vectors for efficient storage and manipulation of elements
  5. Understanding the concept of square roots and fast algorithms to calculate them efficiently
  6. Familiarity with the Sieve of Eratosthenes algorithm, an ancient method for finding all prime numbers up to a given limit

Core Concept

To check whether a number can be expressed as the sum of two prime numbers, we'll first create a function to determine if a given number is prime:

bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) return false;
}
return true;
}

Now, to check whether a number can be expressed as the sum of two prime numbers, we'll create another function that iterates through all possible combinations of prime numbers less than or equal to the square root of the given number. To optimize this process, we'll use the Sieve of Eratosthenes algorithm to generate a list of primes more efficiently:

void sieveOfEratosthenes(int limit) {
vector<bool> primes(limit + 1, true); // Initialize an array to store whether each number is prime or not
primes[0] = primes[1] = false; // 0 and 1 are not prime numbers

for (int p = 2; p * p <= limit; ++p) {
if (primes[p]) {
for (int i = p * p; i <= limit; i += p) primes[i] = false; // Mark multiples of the current prime as not prime
}
}
}

bool isExpressibleAsSumOfTwoPrimes(int num) {
if (num <= 2) return false; // Numbers less than 3 cannot be expressed as sum of two primes

sieveOfEratosthenes(sqrt(num)); // Generate a list of primes up to the square root of the given number

for (int p1 = 2; p1 < sqrt(num); ++p1) {
if (primes[p1]) {
int remaining = num - p1;
if (remaining > p1 && primes[remaining]) return true;
}
}

for (int p1 = 2; p1 * p1 <= num; ++p1) { // Check for square numbers as well
int sqr = p1 * p1;
if (sqr == num) continue; // If it's a square number, skip to the next iteration
if (primes[p1] && primes[num - sqr]) return true;
}

return false; // If no combination of prime numbers less than the square root of the number can sum to the given number, it cannot be expressed as the sum of two primes
}

Worked Example

Let's test our function with a few examples:

#include <iostream>
#include <vector>
using namespace std;

bool isPrime(int num) {
// ... (same as above)
}

void sieveOfEratosthenes(int limit) {
// ... (same as above)
}

bool isExpressibleAsSumOfTwoPrimes(int num) {
// ... (same as above)
}

int main() {
int num1 = 7, num2 = 9, num3 = 10, num4 = 8;

cout << "7 can be expressed as the sum of two primes: " << isExpressibleAsSumOfTwoPrimes(num1) << endl;
cout << "9 can be expressed as the sum of two primes: " << isExpressibleAsSumOfTwoPrimes(num2) << endl;
cout << "10 can be expressed as the sum of two primes: " << isExpressibleAsSumOfTwoPrimes(num3) << endl;
cout << "8 can be expressed as the sum of two primes: " << isExpressibleAsSumOfTwoPrimes(num4) << endl;

return 0;
}

Output:

7 can be expressed as the sum of two primes: true
9 can be expressed as the sum of two primes: false
10 can be expressed as the sum of two primes: true
8 can be expressed as the sum of two primes: false

Common Mistakes

  1. Not initializing the primes vector: Make sure to initialize the vector primes(num + 1, true); before using it.
  2. Forgetting to mark multiples of prime numbers as not prime: In the isPrime() function, ensure that you mark all multiples of a prime number as not prime after finding a prime number.
  3. Not checking for square numbers: When iterating through possible combinations in the isExpressibleAsSumOfTwoPrimes() function, make sure to skip checking for square numbers since they cannot be expressed as the sum of two prime numbers.
  4. Incorrectly calculating the square root: Use the fast sqrt algorithm to calculate the square root more efficiently.
  5. Not considering odd square numbers: In the isExpressibleAsSumOfTwoPrimes() function, check for odd square numbers as well since they can be expressed as the sum of two prime numbers (e.g., 1 = 1 + 0, 9 = 3 + 6).
  6. Not using the Sieve of Eratosthenes algorithm to optimize the isPrime() function: Implementing an optimized version of the isPrime() function using the Sieve of Eratosthenes can significantly improve the efficiency of your program.

Practice Questions

  1. Write a C++ program that checks whether a given number can be expressed as the difference of two prime numbers.
  2. Modify the isExpressibleAsSumOfTwoPrimes() function to check if a number can be expressed as the sum of three prime numbers.
  3. Implement an optimized version of the isPrime() function using the Sieve of Eratosthenes algorithm.
  4. Write a C++ program that finds all prime numbers less than or equal to 100 and checks if each can be expressed as the sum of two prime numbers.
  5. Modify the isExpressibleAsSumOfTwoPrimes() function to check if a number can be expressed as the sum of an even number of prime numbers (e.g., 6 = 3 + 3).

FAQ

Why do we need to mark multiples of prime numbers as not prime in the isPrime() function?

Marking multiples of a prime number as not prime helps us avoid unnecessary checks for composite numbers that are multiples of smaller primes. This optimization makes our function run faster and more efficiently.

Why can't square numbers be expressed as the sum of two prime numbers?

Square numbers cannot be expressed as the sum of two prime numbers because any even square number greater than 4 has an even number of factors, and it is impossible to find two distinct prime factors for such a number. Odd square numbers are perfect squares (e.g., 1, 9, 25, etc.), which have only one unique factor—their square root.

What is the Sieve of Eratosthenes algorithm, and how can it be used to optimize the isPrime() function?

The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to a given limit. The algorithm works by iteratively marking the multiples of each prime number as composite (not prime) and then moving on to the next unmarked number. This method allows us to optimize the isPrime() function by generating a list of primes more efficiently, which can help reduce the time complexity of our program.

Why is it important to check for odd square numbers in the isExpressibleAsSumOfTwoPrimes() function?

Odd square numbers are perfect squares (e.g., 1, 9, 25, etc.), which have only one unique factor—their square root. Since their square roots are prime numbers, we can express odd square numbers as the sum of two prime numbers by taking the square root and adding or subtracting it from itself. For example, 9 = 3² can be expressed as 3 + 3 (or 3 - 3).

Why is it important to use the Sieve of Eratosthenes algorithm in the isExpressibleAsSumOfTwoPrimes() function?

Using the Sieve of Eratosthenes algorithm can significantly improve the efficiency of our program by generating a list of primes more efficiently, which allows us to check whether a given number can be expressed as the sum of two prime numbers in a more optimized manner. This optimization reduces the time complexity of our program and makes it faster for larger inputs.

Check Whether a Number can be Express as Sum of Two Prime Numbers (C++) | C++ | XQA Learn