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.
-
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++ 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;
} |
// 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;
} |
// 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 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# 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 |
// 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
|
<?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 ?> |
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:
#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;
} |
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" );
}
}
} |
import math
# Function to generate all prime numbers up to n def generatePrimes(n):
# Create a boolean array "isPrime[0..n]" and initialize all entries as True
isPrime = [ True ] * (n + 1 )
primes = []
# A value in isPrime[i] will finally be False if i is Not a prime, else True.
for i in range ( 2 , n + 1 ):
if isPrime[i]:
# Append prime number to list
primes.append(i)
# Mark multiples of i as not prime
for j in range (i * i, n + 1 , i):
isPrime[j] = False
return primes
# Function to check if a prime number can be expressed as the sum of two prime numbers def isPossible(n):
# Generate all primes up to n
primes = generatePrimes(n)
# Iterate over all prime numbers
for i in range ( len (primes)):
# Get the difference between n and the current prime number
diff = n - primes[i]
if diff < 2 :
break
isDiffPrime = True
# Check if the difference is a prime number
for j in range ( 2 , int (math.sqrt(diff)) + 1 ):
if diff % j = = 0 :
isDiffPrime = False
break
if isDiffPrime:
return True
return False
# Driver code n = 13
if isPossible(n):
print ( "Yes" ) # If n can be represented as the sum of two primes
else :
print ( "No" ) # If n cannot be represented as the sum of two primes
# This Code Is Contributed By Shubham Tiwari. |
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" );
}
}
} |
// Function to generate all prime numbers up to n function generatePrimes(n) {
// Initialize an array to mark whether a number is prime or not
let isPrime = new Array(n + 1).fill( true );
let primes = [];
// Sieve of Eratosthenes algorithm to find prime numbers
for (let i = 2; i <= n; i++) {
if (isPrime[i]) {
primes.push(i);
for (let 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 function isPossible(n) {
let primes = generatePrimes(n);
for (let i = 0; i < primes.length; i++) {
let diff = n - primes[i];
// If the difference is less than 2, it means
// there are no two prime numbers that sum up to n
if (diff < 2) {
break ;
}
let isDiffPrime = true ;
// Check if the difference is prime
for (let j = 2; j <= Math.sqrt(diff); j++) {
if (diff % j === 0) {
isDiffPrime = false ;
break ;
}
}
if (isDiffPrime) {
return true ;
}
}
return false ;
} // Driver code let n = 13; if (isPossible(n)) {
console.log( "Yes" );
} else {
console.log( "No" );
} |
Output:
Yes
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)