给定一个整数N ,任务是打印GCD和LCM之和等于N的任意一对整数。
例子:
Input: N = 14
Output: 1, 13
Explanation:
For the given pair we have GCD(1, 13) = 1 and LCM (1, 13) = 13. Sum of GCD and LCM = 1 + 13 = 14.
Input: 2
Output: 1 1
Explanation:
For the given pair we have GCD(1, 1) = 1 and LCM (1, 1) = 1. Sum of GCD and LCM = 1 + 1 = 2.
方法:
为了解决上述问题,让我们考虑该对为(1,n-1)。 (1,n-1)的GCD = 1,(1,n-1)的LCM = n – 1 。因此,GCD和LCM的总和= 1 +(n – 1)= n。因此,对(1,n – 1)将是GCD和LCM之和等于N的对。
下面是上述方法的实现:
C++
// C++ implementation to Print any pair of integers
// whose summation of GCD and LCM is equal to integer N
#include
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
输出:
1 13
时间复杂度: O(1)
辅助空间: O(1)