Dado un entero N , la tarea es imprimir cualquier par de enteros que tengan la suma de MCD y MCM igual a N.
Ejemplos:
Entrada: N = 14
Salida: 1, 13
Explicación :
Para el par dado tenemos MCD(1, 13) = 1 y MCM (1, 13) = 13. Suma de MCD y MCM = 1 + 13 = 14.Entrada: 2
Salida: 1 1
Explicación :
Para el par dado tenemos MCD(1, 1) = 1 y MCM (1, 1) = 1. Suma de MCD y MCM = 1 + 1 = 2.
Enfoque:
Para resolver el problema mencionado anteriormente, consideremos que el par es (1, n-1). MCD de (1, n-1) = 1 y MCM de (1, n-1) = n – 1 . Entonces la suma de MCD y MCM = 1 + (n – 1) = n. Por lo tanto, el par (1, n – 1) será el par que tiene la suma de MCD y LCM igual a N.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to Print any pair of integers // whose summation of GCD and LCM is equal to integer N #include <bits/stdc++.h> using namespace std; // Function to print the required pair void printPair(int n) { // print the pair cout << 1 << " " << n - 1; } // Driver code int main() { int n = 14; printPair(n); return 0; }
Java
// Java implementation to print any pair of integers // whose summation of GCD and LCM is equal to integer N class GFG{ // Function to print the required pair static void printPair(int n) { // Print the pair System.out.print(1 + " " + (n - 1)); } // Driver code public static void main(String[] args) { int n = 14; printPair(n); } } // This code is contributed by gauravrajput1
Python3
# Python3 implementation to print any # pair of integers whose summation of # GCD and LCM is equal to integer N # Function to print the required pair def printPair(n): # Print the pair print("1", end = " ") print(n - 1) # Driver code n = 14 printPair(n) # This code is contributed by PratikBasu
C#
// C# implementation to print any pair // of integers whose summation of // GCD and LCM is equal to integer N using System; public class GFG{ // Function to print the required pair static void printPair(int n) { // Print the pair Console.Write(1 + " " + (n - 1)); } // Driver code public static void Main(String[] args) { int n = 14; printPair(n); } } // This code is contributed by Princi Singh
Javascript
<script> // javascript implementation to print any pair of integers // whose summation of GCD and LCM is equal to integer N // Function to print the required pair function printPair(n) { // Print the pair document.write(1 + " " + (n - 1)); } // Driver code var n = 14; printPair(n); // This code is contributed by aashish1995 </script>
1 13
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)