Open In App

Express an odd number as sum of prime numbers

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. 
 




// CPP program to express N as sum of at-most
// three prime numbers.
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a number is prime or not.
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;
}
 
// Prints at most three prime numbers whose
// sum is n.
void findPrimes(int n)
{
    if (isPrime(n)) // CASE-I   
        cout << n << endl;
     
    else if (isPrime(n - 2)) // CASE-II   
        cout << 2 << " " << n - 2 << endl;
 
    else // CASE-III
    {
        cout << 3 << " ";
        n = n - 3;
        for (int i = 0; i < n; i++) {
            if (isPrime(i) && isPrime(n - i)) {
                cout << i << " " << (n - i);
                break;
            }
        }
    }
}
 
// Driver code
int main()
{
    int n = 27;
    findPrimes(n);
    return 0;
}




// Java program to express N as sum
// of at-most three prime numbers.
import java.util.*;
 
class GFG{
     
    // Function to check if a
    // number is prime or not.
    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;
    }
 
     
    // Prints at most three prime
    // numbers whose sum is n.
    public static void findPrimes(int n)
    {
        if (isPrime(n)) // CASE-I
            System.out.print( n );
     
        else if (isPrime(n - 2)) // CASE-II
            System.out.print( 2 + " " +
                              (n - 2) );
 
        else // CASE-III
        {
            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;
                }
            }
        }
    }
 
    // driver code
    public static void main(String[] args)
    {
        int n = 27;
        findPrimes(n);
    }
}
 
// This code is contributed by rishabh_jain




# Python3 program to express N as
# sum of at-most three prime numbers
 
# Function to check if a number
# is prime or not.
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
 
# Prints at most three prime numbers
# whose sum is n.
def findPrimes(n) :
    if (isPrime(n)):
         
        # CASE-I
        print(n, end = " ")
     
    elif (isPrime(n - 2)) :
         
        # CASE-II
        print ("2", end = " ")
        print (n - 2, end = " " )
 
    else:
        #CASE-III
        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
 
# Driver Code
n = 27;
findPrimes(n);
 
# This code is contributed by rishabh_jain




// C# program to express N as sum
// of at-most three prime numbers.
using System;
 
class GFG
{
     
    // Function to check if a
    // number is prime or not.
    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;
    }
 
     
    // Prints at most three prime
    // numbers whose sum is n.
    public static void findPrimes(int n)
    {
        if (isPrime(n)) // CASE-I
            Console.WriteLine( n );
     
        else if (isPrime(n - 2)) // CASE-II
            Console.Write( 2 + " " +
                            (n - 2) );
 
        else // CASE-III
        {
            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;
                }
            }
        }
    }
 
    // Driver code
    public static void Main()
    {
        int n = 27;
        findPrimes(n);
    }
}
 
// This code is contributed by vt_m




<?php
// PHP program to express
// N as sum of at-most
// three prime numbers.
 
// Function to check if a
// number is prime or not.
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;
}
 
// Prints at most three prime
// numbers whose sum is n.
function findPrimes($n)
{
    // CASE-I
    if (isPrime($n))
        echo($n);
     
    // CASE-II
    else if (isPrime($n - 2)) 
        echo(2 . " " . ($n - 2));
 
    // CASE-III
    else
    {
        echo(3 . " ");
        $n = $n - 3;
        for ($i = 0; $i < $n; $i++)
        {
            if (isPrime($i) &&
                isPrime($n - $i))
            {
                echo($i . " " .
                    ($n - $i));
                break;
            }
        }
    }
}
 
// Driver code
$n = 27;
findPrimes($n);
 
// This code is contributed by Ajit.
?>




<script>
// javascript program to express N as sum
// of at-most three prime numbers.
 
    // Function to check if a
    // number is prime or not.
    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;
    }
   
       
    // Prints at most three prime
    // numbers whose sum is n.
    function findPrimes(n)
    {
        if (isPrime(n)) // CASE-I
           document.write( n );
       
        else if (isPrime(n - 2)) // CASE-II
            document.write( 2 + " " +
                              (n - 2) );
   
        else // CASE-III
        {
            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;
                }
            }
        }
    }
 
// Driver code
         let n = 27;
        findPrimes(n);
    
   // This code is contributed by susmitakundugoaldanga.
</script>

Output
3 5 19

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.


Article Tags :