给定整数N。任务是找到一对大于1的N的互质因数。如果不存在这样的因数,则打印’-1’。
例子:
Input: N = 45
Output: 3 5
Explanation: Since 3 and 5 are divisors of 45 and gcd( 3, 5 ) = 1 .
Hence, they satisfy the condition.
Input: N = 25
Output: -1
Explanation: No pair of divisors of 25 satisfy the condition such
that their gcd is 1.
方法:
- 从x = 2迭代到sqrt(N),以找到N的所有除数
- 对于x的任何值,请检查是否将其除以N
- 如果它被除,那么只要将N整除,就将其除以x。
- 现在,检查N> 1,则除数对(x,N)将具有
gcd(x,N)== 1,因为已从N中消除了所有’x’因子。 - 同样,检查x的所有值。
下面是上述方法的实现:
C++
// C++ program to find two coprime
// divisors of a given number
// such that both are greater
// than 1
#include
using namespace std;
// Function which finds the
// required pair of divisors
// of N
void findCoprimePair(int N)
{
// We iterate upto sqrt(N)
// as we can find all the
// divisors of N in this
// time
for (int x = 2; x <= sqrt(N); x++) {
if (N % x == 0) {
// If x is a divisor of N
// keep dividing as long
// as possible
while (N % x == 0) {
N /= x;
}
if (N > 1) {
// We have found a
// required pair
cout << x << " "
<< N << endl;
return;
}
}
}
// No such pair of divisors
// of N was found, hence
// print -1
cout << -1 << endl;
}
// Driver Code
int main()
{
// Sample example 1
int N = 45;
findCoprimePair(N);
// Sample example 2
N = 25;
findCoprimePair(N);
return 0;
}
Java
// Java program to find two coprime
// divisors of a given number
// such that both are greater
// than 1
import java.util.*;
class GFG{
// Function which finds the
// required pair of divisors
// of N
public static void findCoprimePair(int N)
{
// We iterate upto sqrt(N)
// as we can find all the
// divisors of N in this
// time
for(int x = 2; x <= Math.sqrt(N); x++)
{
if (N % x == 0)
{
// If x is a divisor of N
// keep dividing as long
// as possible
while (N % x == 0)
{
N /= x;
}
if (N > 1)
{
// We have found a
// required pair
System.out.println(x + " " + N);
return;
}
}
}
// No such pair of divisors
// of N was found, hence
// print -1
System.out.println(-1);
}
// Driver code
public static void main(String[] args)
{
// Sample example 1
int N = 45;
findCoprimePair(N);
// Sample example 2
N = 25;
findCoprimePair(N);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program to find two coprime
# divisors of a given number such that
# both are greater than 1
import math
# Function which finds the
# required pair of divisors
# of N
def findCoprimePair(N):
# We iterate upto sqrt(N)
# as we can find all the
# divisors of N in this
# time
for x in range(2, int(math.sqrt(N)) + 1):
if (N % x == 0):
# If x is a divisor of N
# keep dividing as long
# as possible
while (N % x == 0):
N //= x
if (N > 1):
# We have found a
# required pair
print(x, N)
return;
# No such pair of divisors
# of N was found, hence
# print -1
print("-1")
# Driver Code
# Sample example 1
N = 45
findCoprimePair(N)
# Sample example 2
N = 25
findCoprimePair(N)
# This code is contributed by Vishal Maurya.
C#
// C# program to find two coprime
// divisors of a given number
// such that both are greater
// than 1
using System;
class GFG{
// Function which finds the
// required pair of divisors
// of N
public static void findCoprimePair(int N)
{
// We iterate upto sqrt(N)
// as we can find all the
// divisors of N in this
// time
for(int x = 2;
x <= Math.Sqrt(N); x++)
{
if (N % x == 0)
{
// If x is a divisor of N
// keep dividing as long
// as possible
while (N % x == 0)
{
N /= x;
}
if (N > 1)
{
// We have found a
// required pair
Console.WriteLine(x +
" " + N);
return;
}
}
}
// No such pair of divisors
// of N was found, hence
// print -1
Console.WriteLine(-1);
}
// Driver code
public static void Main(String[] args)
{
// Sample example 1
int N = 45;
findCoprimePair(N);
// Sample example 2
N = 25;
findCoprimePair(N);
}
}
// This code is contributed by Chitranayal
Javascript
输出:
3 5
-1
时间复杂度:O( sqrt(N) )