Dado un número entero N , la tarea es representar N como la suma de dos números enteros compuestos. Puede haber múltiples formas posibles, imprima cualquiera de ellas. Si no es posible representar el número como la suma de dos números compuestos, imprima -1 .
Ejemplos:
Entrada: N = 13
Salida: 4 9
4 + 9 = 13 y tanto 4 como 9 son compuestos.
Entrada: N = 18
Salida: 4 14
Enfoque: cuando N ≤ 11 , solo 8 y 10 son los números enteros que se pueden representar como la suma de dos números enteros compuestos, es decir, 4 + 4 y 4 + 6 respectivamente.
Cuando N > 11 entonces hay dos casos:
- Cuando N es par: N se puede representar como 4 + (N – 4) ya que ambos son compuestos.
- Cuando N es impar: N se puede representar como 9 + (N – 9) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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
<script> // javascript implementation of the approach // Function to find two composite // numbers which when added // give sum as n function findNums(n) { // Only 8 and 10 can be represented // as the sum of two composite integers if (n <= 11) { if (n == 8) document.write("4 4"); if (n == 10) document.write("4 6"); else document.write("-1"); return; } // If n is even if (n % 2 == 0) document.write("4 " + (n - 4)); // If n is odd else document.write("9 " + (n - 9)); } // Driver code var n = 13; findNums(n); // This code contributed by shikhasingrajput </script>
9 4
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA