N expressed as sum of 4 prime numbers
Express a given number as a summation of 4 positive primes. If it is not possible to express then print “-1”.
Examples:
Input: 24
Output: 3 11 3 7
Explanation : 3+11+3+7 = 24 and 3, 11, 7 are all prime.
Input: 46
Output: 11 11 17 7
explanation : 11+11+17+7 = 46 and 11, 7, 17 are all prime.
Approach : Every even integer greater than 2 can be expressed as the sum of two numbers by Goldbach’s conjecture.
Below are some facts for expressing a number as sum of 4 primes.
- Number must be greater than or equal to 8 as 2 is the smallest prime
- If given number is even, we can break it as (2 + 2) + x so that x remains even and can broken into two primes.
- If given number is odd, we can break it as (2 + 3) + x so that x remains even and can broken into two primes.
Now we can easily express n as sum of two primes using link
C++
#include <bits/stdc++.h>
using namespace std;
int isPrime( int x)
{
int s = sqrt (x);
for ( int i = 2; i <= s; i++)
if (x % i == 0)
return 0;
return 1;
}
void Num( int x, int & a, int & b)
{
for ( int i = 2; i <= x / 2; i++) {
if (isPrime(i) && isPrime(x - i)) {
a = i;
b = x - i;
return ;
}
}
}
void generate( int n)
{
if (n <= 7)
cout << "Impossible to form" << endl;
int a, b;
if (n % 2 != 0) {
Num(n - 5, a, b);
cout << "2 3 " << a << " " << b << endl;
}
else {
Num(n - 4, a, b);
cout << "2 2 " << a << " " << b << endl;
}
}
int main()
{
int n = 28;
generate(n);
return 0;
}
|
Java
class GFG {
static int a = 0 , b = 0 ;
static int isPrime( int x)
{
int s = ( int )Math.sqrt(x);
for ( int i = 2 ; i <= s; i++)
if (x % i == 0 )
return 0 ;
return 1 ;
}
static void Num( int x)
{
for ( int i = 2 ; i <= x / 2 ; i++) {
if (isPrime(i) != 0 && isPrime(x - i) != 0 ) {
a = i;
b = x - i;
return ;
}
}
}
static void generate( int n)
{
if (n <= 7 )
System.out.println( "Impossible"
+ " to form" );
if (n % 2 != 0 ) {
Num(n - 5 );
System.out.println( "2 3 " + a + " " + b);
}
else {
Num(n - 4 );
System.out.println( "2 2 " + a + " " + b);
}
}
public static void main(String[] args)
{
int n = 28 ;
generate(n);
}
}
|
Python3
import math;
def isPrime(x):
s = int (math.sqrt(x))
for i in range ( 2 ,s + 1 ):
if (x % i = = 0 ):
return 0
return 1
def Num(x):
ab = [ 0 ] * 2
for i in range ( 2 , int (x / 2 ) + 1 ):
if (isPrime(i) ! = 0 and isPrime(x - i) ! = 0 ):
ab[ 0 ] = i
ab[ 1 ] = x - i
return ab
def generate(n):
if (n < = 7 ):
print ( "Impossible to form" )
if (n % 2 ! = 0 ):
ab = Num(n - 5 )
print ( "2 3" ,ab[ 0 ],ab[ 1 ])
else :
ab = Num(n - 4 )
print ( "2 2" ,ab[ 0 ],ab[ 1 ])
if __name__ = = '__main__' :
n = 28
generate(n)
|
C#
using System;
class GFG {
static int a = 0, b = 0;
static int isPrime( int x)
{
int s = ( int )Math.Sqrt(x);
for ( int i = 2; i <= s; i++)
if (x % i == 0)
return 0;
return 1;
}
static void Num( int x)
{
for ( int i = 2; i <= x / 2; i++)
{
if (isPrime(i) != 0 &&
isPrime(x - i) != 0)
{
a = i;
b = x - i;
return ;
}
}
}
static void generate( int n)
{
if (n <= 7)
Console.Write( "Impossible"
+ " to form" );
if (n % 2 != 0) {
Num(n - 5);
Console.Write( "2 3 " + a + " "
+ b);
}
else {
/// calls the function to get
Num(n - 4);
Console.Write( "2 2 " + a + " "
+ b);
}
}
public static void Main()
{
int n = 28;
generate(n);
}
}
|
PHP
Javascript
Output:
2 2 5 19
Time complexity: O(n sqrt(n))
Auxiliary space: O(1)
This article is contributed by Raja Vikramaditya
Last Updated :
18 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...