Given an odd number, we need to express it as the sum of at most three prime numbers.
Examples :
Input: 27
Output: 27 = 3 + 5 + 19
Input: 15
Output: 15 = 2 + 13
Approach : Here, we use Goldbach’s conjecture to solve this problem. It says that any even integer can be expressed as sum of two prime numbers.
We have three cases here:
1) When N is a prime number, print the number.
2) When (N-2) is a prime number, print 2 and N-2.
3) Express N as 3 + (N-3). Obviously, N-3 will be an even number (subtraction of an odd from another odd results in even). So, according to Goldbach’s conjecture, it can be expressed as the sum of two prime numbers. So, print 3 and other two prime numbers.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int x)
{
if (x == 0 || x == 1)
return false ;
for ( int i = 2; i * i <= x; ++i)
if (x % i == 0)
return false ;
return true ;
}
void findPrimes( int n)
{
if (isPrime(n))
cout << n << endl;
else if (isPrime(n - 2))
cout << 2 << " " << n - 2 << endl;
else
{
cout << 3 << " " ;
n = n - 3;
for ( int i = 0; i < n; i++) {
if (isPrime(i) && isPrime(n - i)) {
cout << i << " " << (n - i);
break ;
}
}
}
}
int main()
{
int n = 27;
findPrimes(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static boolean isPrime( int x)
{
if (x == 0 || x == 1 )
return false ;
for ( int i = 2 ; i * i <= x; ++i)
if (x % i == 0 )
return false ;
return true ;
}
public static void findPrimes( int n)
{
if (isPrime(n))
System.out.print( n );
else if (isPrime(n - 2 ))
System.out.print( 2 + " " +
(n - 2 ) );
else
{
System.out.print( 3 + " " );
n = n - 3 ;
for ( int i = 0 ; i < n; i++) {
if (isPrime(i) && isPrime(n - i)) {
System.out.print( i + " " +
(n - i));
break ;
}
}
}
}
public static void main(String[] args)
{
int n = 27 ;
findPrimes(n);
}
}
|
Python3
def isPrime(x):
if (x = = 0 or x = = 1 ) :
return 0
i = 2
while i * i < = x :
if (x % i = = 0 ) :
return 0
i = i + 1
return 1
def findPrimes(n) :
if (isPrime(n)):
print (n, end = " " )
elif (isPrime(n - 2 )) :
print ( "2" , end = " " )
print (n - 2 , end = " " )
else :
print ( "3" , end = " " )
n = n - 3
i = 0
while i < n :
if (isPrime(i) and isPrime(n - i)) :
print (i, end = " " )
print ((n - i), end = " " )
break
i = i + 1
n = 27 ;
findPrimes(n);
|
C#
using System;
class GFG
{
public static bool isPrime( int x)
{
if (x == 0 || x == 1)
return false ;
for ( int i = 2; i * i <= x; ++i)
if (x % i == 0)
return false ;
return true ;
}
public static void findPrimes( int n)
{
if (isPrime(n))
Console.WriteLine( n );
else if (isPrime(n - 2))
Console.Write( 2 + " " +
(n - 2) );
else
{
Console.Write( 3 + " " );
n = n - 3;
for ( int i = 0; i < n; i++) {
if (isPrime(i) && isPrime(n - i))
{
Console.WriteLine( i + " " +
(n - i));
break ;
}
}
}
}
public static void Main()
{
int n = 27;
findPrimes(n);
}
}
|
PHP
<?php
function isPrime( $x )
{
if ( $x == 0 || $x == 1)
return false;
for ( $i = 2; $i * $i <= $x ; ++ $i )
if ( $x % $i == 0)
return false;
return true;
}
function findPrimes( $n )
{
if (isPrime( $n ))
echo ( $n );
else if (isPrime( $n - 2))
echo (2 . " " . ( $n - 2));
else
{
echo (3 . " " );
$n = $n - 3;
for ( $i = 0; $i < $n ; $i ++)
{
if (isPrime( $i ) &&
isPrime( $n - $i ))
{
echo ( $i . " " .
( $n - $i ));
break ;
}
}
}
}
$n = 27;
findPrimes( $n );
?>
|
Javascript
<script>
function isPrime(x)
{
if (x == 0 || x == 1)
return false ;
for (let i = 2; i * i <= x; ++i)
if (x % i == 0)
return false ;
return true ;
}
function findPrimes(n)
{
if (isPrime(n))
document.write( n );
else if (isPrime(n - 2))
document.write( 2 + " " +
(n - 2) );
else
{
document.write( 3 + " " );
n = n - 3;
for (let i = 0; i < n; i++) {
if (isPrime(i) && isPrime(n - i)) {
document.write( i + " " +
(n - i));
break ;
}
}
}
}
let n = 27;
findPrimes(n);
</script>
|
Time complexity: O(n?n), (?n) to check if the number is prime and n numbers are checked.
Auxiliary space: O(1) as no extra space is used.