Table of contents
1.
Problem
2.
Solution
2.1.
Naive Approach:
2.2.
Efficient Approach: 
3.
FAQs
4.
Key takeaways
Last Updated: Mar 27, 2024

Prime Numbers from 1 to N that can be Represented as a Sum of Two Prime Numbers

Author GAZAL ARORA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Problem

Given a positive number N, print the count of prime numbers from 1 to N that can be written as a sum of two prime numbers.

Prime numbers are the numbers greater than 1 and have exactly two factors, 1 and themselves.

For example, 2, 3, 5, 7, etc., are the prime numbers.

 

Input: An integer N.

Output: An integer.

The questions you should ask the interviewer:

  1. What is the constraint on N?

 

For example,

1. 

Input: N = 7.

Output: 2.

Explanation:

5 and 7 are the prime numbers over the range [1, 7] that can be represented as a sum of two prime numbers, i.e., 5 = 2 + 3, and 7 = 2 + 5, where 2, 3, 5, and 7 are all primes.

 

2.

Input: N = 15.

Output: 3.

Explanation: 5(2 + 3), 7(2 + 5), and 13(2 + 11) are the prime numbers over the range [1, 15] that can be represented as a sum of two prime numbers.

 

Recommended: Please try to solve it yourself before moving on to the solution.

Solution

Naive Approach:

One basic approach would be to consider all pairs (i, j) over the range [1, N] and if i,  j, and (i+j) are prime numbers and (i + j) lies in the range [1, N], increment the count. After checking for all possible pairs, print the count.

Time Complexity: O(N3). One for loop is used for i in the range [1, N], one for loop for j in the range [1, N], and one loop in the range [1, N] is used to check if i, j, and i+j are prime numbers or not. Hence O(N3) time. 

Space Complexity: O(1).

Let's try to improve time complexity. 

Efficient Approach: 

 

Some observations:

  1. 2 is the only even prime number. All the other prime numbers are odd.
  2. An odd number is always the sum of an even and an odd number.
  3. It is impossible to represent a prime number(which is odd) as a sum of two odd prime numbers. Therefore to represent a prime number as a sum of two prime numbers, one prime number must be even, which is 2 only.
  4. So a prime number X can be represented as a sum of two prime numbers (2 + (X-2)) if X-2 is also prime.

 

Algorithm:

  1. Initialize a dp array of size N+1 where dp[i] will represent the count of prime numbers from a range of [1, i] that can be described as a sum of two prime numbers.
  2. Initialize an array prime of size N+1, and add all the prime numbers till N using the Sieve Of Eratosthenes.
  3. Loop over [2, N] and do the following:
    1. Add previous values in dp[i]: dp[i] = dp[i-1].
    2. Increment dp[i] by 1 if i and i-2 are prime numbers as prime number i can be represented as (2 + ( i-2 )).

 

C++

#include <bits/stdc++.h>
using namespace std;

// Sieve of Eratosthenes to store prime numbers.
void SieveOfEratosthenes(vector<bool> &prime, int n)
{
   // set 0 and 1 as non prime.
    prime[0] = 0;
    prime[1] = 0;
    
    for (int i = 2; i * i <= n; i++) {
    // If p is a prime number.
        if (prime[i]) {

            // Set all multiples of p as a non-prime number.
            for (int m = i * i; m <= n; m += i) {
                prime[m] = false;
            }
        }
     }
}

// Function to count the required prime numbers.
int PrimeNum(int n)
{
    // Stores all the prime numbers.
    vector<bool> prime(n+1, 1);
    SieveOfEratosthenes(prime, n);

    // Create a dp array.
    vector<int> dp(n+1, 0);

    // Iterate over the range of [2, N].
    for (int i = 2; i <= n; i++) {

        // Update dp[i] by adding the previous count value.
        dp[i] = dp[i - 1];

        // Increment dp[i] by 1 if i and (i - 2) are prime numbers.
        if (prime[i] && prime[i - 2] ) {
            dp[i] = dp[i] + 1;
        }
    }
    return dp[n];
}

int main()
{
    int N = 15;
    cout<<PrimeNum(N);
    return 0;
}

 

Input: N = 15.

Output: 3.

Time Complexity: O(N * log(log(N))).

Space Complexity: O(N). Extra space is used by the arrays: prime and dp.

Also see, Euclid GCD Algorithm

FAQs

  1. What is a prime number?
    A prime number is a number greater than 1 and can be divided by 1 or itself only. For example, 2, 5, 7, 11, 13, 17, 19, 23, 29, etc, are the prime numbers.
     
  2. Can any number be represented as the sum of two prime numbers?
    Yes, every number can be expressed as the sum of two primes. It can be written as the sum of as many primes as one wishes. Example, 14 = 3 + 11, 15 = 13 + 2, 16 = 13 + 3, etc,.

Key takeaways

Today, we learned about Prime Numbers and designed an algorithm to count prime numbers from 1 to N that can be expressed as a sum of two prime numbers.

We proposed two approaches for the algorithm:

  1. Approach 1 involves considering all pairs (i, j) over the range [1, N] and checking if i,  j, and (i+j) are prime numbers. It was taking O(N3) time.
  2. In Approach 2, we used Sieve Of Eratosthenes and DP to count the prime numbers, taking O(N * log(log(N))) time which is better than Approach 1.
     

Want to learn more?

You can use Coding Ninjas Studio to practice a wide range of DSA questions asked in various interviews. It will assist you in mastering efficient coding techniques, and you will also get interview experiences from people working in big companies.

 

Live masterclass