Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given a prime number N. The task is to check if it is possible to express N as the sum of two separate prime numbers.
Note: The range of N is less than 108.

Examples: 

Input: N = 13
Output: Yes
Explanation: The number 13 can be written as 11 + 2, 
here 11 and 2 are both prime.

Input: N = 11
Output: No

Simple Solution: A simple solution is to create a sieve to store all the prime numbers less than the number N. Then run a loop from 1 to N and check whether i and n-i are both prime or not. If yes then print Yes, else No.

Time Complexity: O(n)
Space Complexity: O(n)

Efficient solution: Apart from 2, all of the prime numbers are odd. So it is not possible to represent a prime number (which is odd) to be written as a sum of two odd prime numbers, so we are sure that one of the two prime numbers should be 2. So we have to check whether n-2 is prime or not. If it holds we print Yes else No.
For example, if the number is 19 then we have to check whether 19-2 = 17 is a prime number or not. If 17 is a prime number then print yes otherwise print no.

Algorithm:

  •  Create a static function with a boolean return type that takes an integer element as input.
    •  Check if n is less than or equal to 1. If yes, return false as 1 and any number less than 1 is not considered prime.
    •  now start for loop from i=2 to the square root of n and Check if n is divisible by i. If yes, return false as n is not a prime number.
    • and if there is no n which is divided by I then we came out of the loop and return true
  • Step 3: Create a static function names impossible of boolean return type which takes an integer value as input
    • Check if N and N-2 are prime numbers by calling the “isPrime” function. If yes, return true. Otherwise, return false.
       

Below is the implementation of the above approach: 

C




// C program to check if a prime number
// can be expressed as sum of
// two Prime Numbers
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
 
// Function to check whether a number
// is prime or not
bool isPrime(int n)
{
    if (n <= 1)
        return false;
 
    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
            return false;
    }
 
    return true;
}
 
// Function to check if a prime number
// can be expressed as sum of
// two Prime Numbers
bool isPossible(int N)
{
    // if the number is prime,
    // and number-2 is also prime
    if (isPrime(N) && isPrime(N - 2))
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int n = 13;
 
    if (isPossible(n))
        printf("%s", "Yes");
    else
        printf("%s", "No");
 
    return 0;
}

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 whether a number
// is prime or not
bool isPrime(int n)
{
    if (n <= 1)
        return false;
 
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0)
            return false;
    }
 
    return true;
}
 
// Function to check if a prime number
// can be expressed as sum of
// two Prime Numbers
bool isPossible(int N)
{
    // if the number is prime,
    // and number-2 is also prime
    if (isPrime(N) && isPrime(N - 2))
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int n = 13;
 
    if (isPossible(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Java




// Java program to check if a prime number
// can be expressed as sum of
// two Prime Numbers
 
public class GFG{
     
    // Function to check whether a number
    // is prime or not
    static boolean isPrime(int n)
    {
        if (n <= 1)
            return false;
     
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
     
        return true;
    }
     
    // Function to check if a prime number
    // can be expressed as sum of
    // two Prime Numbers
    static boolean isPossible(int N)
    {
        // if the number is prime,
        // and number-2 is also prime
        if (isPrime(N) && isPrime(N - 2))
            return true;
        else
            return false;
    }
     
     // Driver code
     public static void main(String []args){
          
        int n = 13;
     
        if (isPossible(n) == true)
            System.out.println("Yes");
        else
            System.out.println("No");
     }
     // This code is contributed by ANKITRAI1
}

Python3




# Python3 program to check if a prime
# number can be expressed as sum of
# two Prime Numbers
import math
 
# Function to check whether a number
# is prime or not
def isPrime(n):
    if n <= 1:
        return False
     
    if n == 2:
        return True
         
    if n%2 == 0:
        return False
         
    for i in range(3, int(math.sqrt(n))+1, 2):
        if n%i == 0:
            return False
    return True
 
# Function to check if a prime number
# can be expressed as sum of
# two Prime Numbers
def isPossible(n):
 
    # if the number is prime,
    # and number-2 is also prime
    if isPrime(n) and isPrime(n - 2):
        return True
    else:
        return False
 
# Driver code
n = 13
if isPossible(n) == True:
    print("Yes")
else:
    print("No")
     
# This code is contributed by Shrikant13

C#




// C# program to check if a prime
// number can be expressed as sum
// of two Prime Numbers
using System;
 
class GFG
{
 
// Function to check whether a
// number is prime or not
static bool isPrime(int n)
{
    if (n <= 1)
        return false;
 
    for (int i = 2;
             i <= Math.Sqrt(n); i++)
    {
        if (n % i == 0)
            return false;
    }
 
    return true;
}
 
// Function to check if a prime
// number can be expressed as sum
// of two Prime Numbers
static bool isPossible(int N)
{
    // if the number is prime,
    // and number-2 is also prime
    if (isPrime(N) && isPrime(N - 2))
        return true;
    else
        return false;
}
 
// Driver code
public static void Main()
{
    int n = 13;
 
    if (isPossible(n) == true)
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed
// by ChitraNayal

PHP




<?php
// PHP program to check if a prime
// number can be expressed as sum
// of two Prime Numbers
 
// Function to check whether a
// number is prime or not
function isPrime($n)
{
    if ($n <= 1)
        return false;
 
    for ($i = 2; $i <= sqrt($n); $i++)
    {
        if ($n % $i == 0)
            return false;
    }
 
    return true;
}
 
// Function to check if a prime
// number can be expressed as sum
// of two Prime Numbers
function isPossible($N)
{
    // if the number is prime,
    // and number-2 is also prime
    if (isPrime($N) && isPrime($N - 2))
        return true;
    else
        return false;
}
 
// Driver code
$n = 13;
 
if (isPossible($n))
    echo ("Yes");
else
    echo("No");
 
// This code is contributed
// by Shivi_Aggarwal
?>

Javascript




// JavaScript program to check if a prime number
// can be expressed as sum of
// two Prime Numbers
 
// Function to check whether a number
// is prime or not
function isPrime(n) {
    if (n <= 1)
        return false;
    for (let i = 2; i <= Math.sqrt(n); i++) {
        if (n % i == 0)
            return false;
    }
 
    return true;
}
 
// Function to check if a prime number
// can be expressed as sum of
// two Prime Numbers
function isPossible(N) {
    // if the number is prime,
    // and number-2 is also prime
    if (isPrime(N) && isPrime(N - 2))
        return true;
    else
        return false;
}
 
// Driver code
let n = 13;
 
if (isPossible(n))
    console.log("Yes");
else
    console.log("No");
    //This code is contributed by sarojmcy2e

Output: 

Yes

 

Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)

Approach 2: 

Here’s another approach to check if a prime number can be expressed as the sum of two prime numbers:

  • First, generate all prime numbers up to the given number. This can be done using the Sieve of Eratosthenes algorithm.
  • Then, iterate through each prime number and check if the difference between the given number and the current prime number is also a prime number.
  • If both the current prime number and the difference are prime, then the given number can be expressed as the sum of two prime numbers.

Here’s the updated C++ code:

C++




#include <bits/stdc++.h>
using namespace std;
 
// Function to generate all prime numbers up to n
vector<int> generatePrimes(int n) {
    vector<bool> isPrime(n + 1, true);
    vector<int> primes;
    for (int i = 2; i <= n; i++) {
        if (isPrime[i]) {
            primes.push_back(i);
            for (int j = i * i; j <= n; j += i) {
                isPrime[j] = false;
            }
        }
    }
    return primes;
}
 
// Function to check if a prime number can be expressed as the sum of two prime numbers
bool isPossible(int n) {
    vector<int> primes = generatePrimes(n);
    for (int i = 0; i < primes.size(); i++) {
        int diff = n - primes[i];
        if (diff < 2) {
            break;
        }
        bool isDiffPrime = true;
        for (int j = 2; j <= sqrt(diff); j++) {
            if (diff % j == 0) {
                isDiffPrime = false;
                break;
            }
        }
        if (isDiffPrime) {
            return true;
        }
    }
    return false;
}
 
// Driver code
int main() {
    int n = 13;
    if (isPossible(n)) {
        cout << "Yes";
    } else {
        cout << "No";
    }
    return 0;
}

Java




import java.util.ArrayList;
 
public class Main {
 
    // Function to generate all prime numbers up to n
    public static ArrayList<Integer> generatePrimes(int n) {
        boolean[] isPrime = new boolean[n + 1];
        ArrayList<Integer> primes = new ArrayList<>();
        for (int i = 2; i <= n; i++) {
            isPrime[i] = true;
        }
 
        for (int i = 2; i * i <= n; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j <= n; j += i) {
                    isPrime[j] = false;
                }
            }
        }
 
        for (int i = 2; i <= n; i++) {
            if (isPrime[i]) {
                primes.add(i);
            }
        }
 
        return primes;
    }
 
    // Function to check if a prime number can be expressed as the sum of two prime numbers
    public static boolean isPossible(int n) {
        ArrayList<Integer> primes = generatePrimes(n);
        for (int i = 0; i < primes.size(); i++) {
            int diff = n - primes.get(i);
            if (diff < 2) {
                break;
            }
            boolean isDiffPrime = true;
            for (int j = 2; j <= Math.sqrt(diff); j++) {
                if (diff % j == 0) {
                    isDiffPrime = false;
                    break;
                }
            }
            if (isDiffPrime) {
                return true;
            }
        }
        return false;
    }
 
    // Driver code
    public static void main(String[] args) {
        int n = 13;
        if (isPossible(n)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

C#




using System;
using System.Collections.Generic;
 
public class Program {
    // Function to generate all prime numbers up to n
    static List<int> GeneratePrimes(int n) {
        bool[] isPrime = new bool[n + 1];
        List<int> primes = new List<int>();
        for (int i = 2; i <= n; i++) {
            isPrime[i] = true;
        }
        for (int i = 2; i <= n; i++) {
            if (isPrime[i]) {
                primes.Add(i);
                for (int j = i * i; j <= n; j += i) {
                    isPrime[j] = false;
                }
            }
        }
        return primes;
    }
 
    // Function to check if a prime number can be expressed as the sum of two prime numbers
    static bool IsPossible(int n) {
        List<int> primes = GeneratePrimes(n);
        for (int i = 0; i < primes.Count; i++) {
            int diff = n - primes[i];
            if (diff < 2) {
                break;
            }
            bool isDiffPrime = true;
            for (int j = 2; j <= Math.Sqrt(diff); j++) {
                if (diff % j == 0) {
                    isDiffPrime = false;
                    break;
                }
            }
            if (isDiffPrime) {
                return true;
            }
        }
        return false;
    }
 
    // Driver code
    static void Main(string[] args) {
        int n = 13;
        if (IsPossible(n)) {
            Console.WriteLine("Yes");
        } else {
            Console.WriteLine("No");
        }
    }
}

Output: 

Yes

Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Last Updated : 30 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials