Check if a prime number can be expressed as sum of two Prime Numbers
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++
#include <bits/stdc++.h>
using namespace std;
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 ;
}
bool isPossible( int N)
{
if (isPrime(N) && isPrime(N - 2))
return true ;
else
return false ;
}
int main()
{
int n = 13;
if (isPossible(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
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 ;
}
bool isPossible( int N)
{
if (isPrime(N) && isPrime(N - 2))
return true ;
else
return false ;
}
int main()
{
int n = 13;
if (isPossible(n))
printf ( "%s" , "Yes" );
else
printf ( "%s" , "No" );
return 0;
}
|
Java
public class GFG{
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 ;
}
static boolean isPossible( int N)
{
if (isPrime(N) && isPrime(N - 2 ))
return true ;
else
return false ;
}
public static void main(String []args){
int n = 13 ;
if (isPossible(n) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import math
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
def isPossible(n):
if isPrime(n) and isPrime(n - 2 ):
return True
else :
return False
n = 13
if isPossible(n) = = True :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
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 ;
}
static bool isPossible( int N)
{
if (isPrime(N) && isPrime(N - 2))
return true ;
else
return false ;
}
public static void Main()
{
int n = 13;
if (isPossible(n) == true )
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
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 isPossible(N) {
if (isPrime(N) && isPrime(N - 2))
return true ;
else
return false ;
}
let n = 13;
if (isPossible(n))
console.log( "Yes" );
else
console.log( "No" );
|
PHP
<?php
function isPrime( $n )
{
if ( $n <= 1)
return false;
for ( $i = 2; $i <= sqrt( $n ); $i ++)
{
if ( $n % $i == 0)
return false;
}
return true;
}
function isPossible( $N )
{
if (isPrime( $N ) && isPrime( $N - 2))
return true;
else
return false;
}
$n = 13;
if (isPossible( $n ))
echo ( "Yes" );
else
echo ( "No" );
?>
|
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;
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;
}
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 ;
}
int main() {
int n = 13;
if (isPossible(n)) {
cout << "Yes" ;
} else {
cout << "No" ;
}
return 0;
}
|
Java
import java.util.ArrayList;
public class Main {
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;
}
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 ;
}
public static void main(String[] args) {
int n = 13 ;
if (isPossible(n)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
import math
def generatePrimes(n):
isPrime = [ True ] * (n + 1 )
primes = []
for i in range ( 2 , n + 1 ):
if isPrime[i]:
primes.append(i)
for j in range (i * i, n + 1 , i):
isPrime[j] = False
return primes
def isPossible(n):
primes = generatePrimes(n)
for i in range ( len (primes)):
diff = n - primes[i]
if diff < 2 :
break
isDiffPrime = True
for j in range ( 2 , int (math.sqrt(diff)) + 1 ):
if diff % j = = 0 :
isDiffPrime = False
break
if isDiffPrime:
return True
return False
n = 13
if isPossible(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
public class Program {
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;
}
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 ;
}
static void Main( string [] args) {
int n = 13;
if (IsPossible(n)) {
Console.WriteLine( "Yes" );
} else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
function generatePrimes(n) {
let isPrime = new Array(n + 1).fill( true );
let primes = [];
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 isPossible(n) {
let primes = generatePrimes(n);
for (let i = 0; i < primes.length; i++) {
let diff = n - primes[i];
if (diff < 2) {
break ;
}
let isDiffPrime = true ;
for (let j = 2; j <= Math.sqrt(diff); j++) {
if (diff % j === 0) {
isDiffPrime = false ;
break ;
}
}
if (isDiffPrime) {
return true ;
}
}
return false ;
}
let n = 13;
if (isPossible(n)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Output:
Yes
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Last Updated :
21 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...