📌  相关文章
📜  求 N 的两个真因数,使得它们的和与 N 互质

📅  最后修改于: 2022-05-13 01:56:07.656000             🧑  作者: Mango

求 N 的两个真因数,使得它们的和与 N 互质

给定一个整数N ,您必须找到N的两个适当因子,使得它们的和与给定整数N互质。如果不存在此类因素,则打印 -1。

例子:

朴素方法:生成 N 的所有适当因子的列表,并为每个可能的对检查它们的总和是否与 N 互质,即 GCD(整数对之和,N) = 1。这里 GCD 表示最大公约数。

有效方法:如果两个数字AB互质,那么它们的和与它们的乘积互质。牢记这一点,找到 N 的所有因子,对于每个因子d1 ,计算与d1互质的 N 的最大因子d2 。要计算 d2,只需将N除以d1直到N%d1 != 0 。最后,检查d1d2是否是N的真因数(即 d1>1 和 d2>1)。

下面是上述方法的实现:

C++
// C++ Program for the above approach
#include 
using namespace std;
 
// Function to find two proper
// factors of N such that their
// sum is coprime with N
void printFactors(int n)
{
 
    // Find factors in sorted order
    for (int i = 2; i <= sqrt(n); i++) {
 
        if (n % i == 0) {
            int d1 = i, d2 = n;
 
            // Find largest value of d2 such
            // that d1 and d2 are co-prime
            while (d2 % d1 == 0) {
                d2 = d2 / d1;
            }
 
            // Check if d1 and d2 are proper
            // factors of N
            if (d1 > 1 && d2 > 1) {
                // Print answer
                cout << d1 << ", " << d2;
                return;
            }
        }
    }
 
    // No such factors exist
    cout << -1;
}
 
// Driver code
int main()
{
    int N = 10;
 
    // Function Call
    printFactors(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find two proper
// factors of N such that their
// sum is coprime with N   
static void printFactors(int n)
{
     
    // Find factors in sorted order
    for(int i = 2; i <= (int)Math.sqrt(n); i++)
    {
        if (n % i == 0)
        {
            int d1 = i, d2 = n;
 
            // Find largest value of d2 such
            // that d1 and d2 are co-prime
            while (d2 % d1 == 0)
            {
                d2 = d2 / d1;
            }
 
            // Check if d1 and d2 are proper
            // factors of N
            if (d1 > 1 && d2 > 1)
            {
                 
                // Print answer
                System.out.print(d1 + ", " + d2);
                return;
            }
        }
    }
 
    // No such factors exist
    System.out.print(-1);
}
 
// Driver code
public static void main(String[] args)
{
    int N = 10;
     
    // Function Call
    printFactors(N);
}
}
 
// This code is contributed by Potta Lokesh


Python3
# Python Program for the above approach
import math
 
# Function to find two proper
# factors of N such that their
# sum is coprime with N
def printFactors(n):
    # Find factors in sorted order
    for i in range(2, int(math.sqrt(n))+1):
        if (n % i == 0):
            d1 = i
            d2 = n
             
            # Find largest value of d2 such
            # that d1 and d2 are co-prime
            while (d2 % d1 == 0):
                d2 = d2 // d1
             
            # Check if d1 and d2 are proper
            # factors of N
            if (d1 > 1 and d2 > 1):
                 
                # Print answer
                print(d1, d2, sep=", ")
                return
    # No such factors exist
    print(-1)
# Driver code
N = 10
 
# Function Call
printFactors(N)
     
# This code is contributed by Shivani


C#
// C# Program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find two proper
// factors of N such that their
// sum is coprime with N
static void printFactors(int n)
{
 
    // Find factors in sorted order
    for (int i = 2; i <= (int)Math.Sqrt(n); i++) {
 
        if (n % i == 0) {
            int d1 = i, d2 = n;
 
            // Find largest value of d2 such
            // that d1 and d2 are co-prime
            while (d2 % d1 == 0) {
                d2 = d2 / d1;
            }
 
            // Check if d1 and d2 are proper
            // factors of N
            if (d1 > 1 && d2 > 1)
            {
               
                // Print answer
                Console.Write(d1 + ", "+d2);
                return;
            }
        }
    }
 
    // No such factors exist
    Console.Write(-1);
}
 
// Driver code
public static void Main()
{
    int N = 10;
   
    // Function Call
    printFactors(N);
}
}
 
// This code is contributed by ipg2016107.


Javascript


输出:
2, 5

时间复杂度: O(√N)
辅助空间: O(1)