📜  将给定数字表示为两个复合数字的总和

📅  最后修改于: 2021-04-26 19:39:24             🧑  作者: Mango

给定一个整数N ,任务是将N表示为两个复合整数的总和。可以有多种方法,可以打印其中任何一种。如果无法将数字表示为两个复合数字的总和,则打印-1
例子:

方法:N≤11时,只有810是整数,可以表示为两个复合整数之和,分别为4 + 44 + 6
N> 11时,有两种情况:

  1. 当N为偶数时: N和N可以表示为4 +(N – 4),因为两者都是复合的。
  2. 当N为奇数时: N可以表示为9 +(N – 9)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find two composite
// numbers which when added
// give sum as n
void findNums(int n)
{
 
    // Only 8 and 10 can be represented
    // as the sum of two composite integers
    if (n <= 11) {
        if (n == 8)
            cout << "4 4";
        if (n == 10)
            cout << "4 6";
        else
            cout << "-1";
        return;
    }
 
    // If n is even
    if (n % 2 == 0)
        cout << "4 " << (n - 4);
 
    // If n is odd
    else
        cout << "9 " << (n - 9);
}
 
// Driver code
int main()
{
    int n = 13;
 
    findNums(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to find two composite
// numbers which when added
// give sum as n
static void findNums(int n)
{
 
    // Only 8 and 10 can be represented
    // as the sum of two composite integers
    if (n <= 11)
    {
        if (n == 8)
            System.out.print("4 4");
        if (n == 10)
            System.out.print("4 6");
        else
            System.out.print("-1");
        return;
    }
 
    // If n is even
    if (n % 2 == 0)
        System.out.print("4 " + (n - 4));
 
    // If n is odd
    else
        System.out.print("9 " + (n - 9));
}
 
// Driver code
public static void main(String args[])
{
    int n = 13;
 
    findNums(n);
}
}
 
// This code is contributed by andrew1234


Python3
# Python3 implementation of the approach
 
# Function to find two composite
# numbers which when added
# give sum as n
def findNums(n):
 
    # Only 8 and 10 can be represented
    # as the sum of two composite integers
    if (n <= 11):
        if (n == 8):
            print("4 4", end = " ")
        if (n == 10):
            print("4 6", end = " ")
        else:
            print("-1", end = " ")
 
    # If n is even
    if (n % 2 == 0):
        print("4 ", (n - 4), end = " ")
 
    # If n is odd
    else:
        print("9 ", n - 9, end = " ")
 
# Driver code
n = 13
 
findNums(n)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to find two composite
    // numbers which when added
    // give sum as n
    static void findNums(int n)
    {
     
        // Only 8 and 10 can be represented
        // as the sum of two composite integers
        if (n <= 11)
        {
            if (n == 8)
                Console.Write("4 4");
            if (n == 10)
                Console.Write("4 6");
            else
                Console.Write("-1");
            return;
        }
     
        // If n is even
        if (n % 2 == 0)
            Console.Write("4 " + (n - 4));
     
        // If n is odd
        else
            Console.Write("9 " + (n - 9));
    }
     
    // Driver code
    public static void Main()
    {
        int n = 13;
     
        findNums(n);
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
9 4