📜  计算所有与N互质数为N的所有对数除数

📅  最后修改于: 2021-05-05 00:41:17             🧑  作者: Mango

给定整数N ,任务是计算N的所有对数,使得每对的和与N互质。
例子:

方法:
为了解决上述问题,我们可以通过找到所有√N复杂度的除数,并检查每对数的和是否与N互质,来轻松计算结果。
下面是上述方法的实现:

C++
// C++ program to count all pairs
// of divisors such that their sum
// is coprime with N
#include 
using namespace std;
 
// Function to calculate GCD
int gcd(int a, int b)
{
    if (b == 0)
        return a;
 
    return (gcd(b, a % b));
}
 
// Function to count all valid pairs
int CountPairs(int n)
{
 
    // Initialize count
    int cnt = 0;
 
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            int div1 = i;
 
            int div2 = n / i;
 
            int sum = div1 + div2;
 
            // Check if sum of pair
            // and n are coprime
            if (gcd(sum, n) == 1)
 
                cnt += 1;
        }
    }
 
    // Return the result
    return cnt;
}
 
// Driver code
int main()
{
 
    int n = 24;
 
    cout << CountPairs(n) << endl;
 
    return 0;
}


Java
// Java program to count all pairs
// of divisors such that their sum
// is coprime with N
import java.util.*;
 
class GFG{
 
// Function to calculate GCD
public static int gcd(int a, int b)
{
    if (b == 0)
        return a;
 
    return (gcd(b, a % b));
}
 
// Function to count all valid pairs
public static int CountPairs(int n)
{
 
    // Initialize count
    int cnt = 0;
 
    for(int i = 1; i * i <= n; i++)
    {
       if (n % i == 0)
       {
           int div1 = i;
           int div2 = n / i;
           int sum = div1 + div2;
            
           // Check if sum of pair
           // and n are coprime
           if (gcd(sum, n) == 1)
               cnt += 1;
       }
    }
 
    // Return the result
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 24;
 
    System.out.println(CountPairs(n));
}
}
 
// This code is contributed by equbalzeeshan


Python3
# Python3 program to count all pairs
# of divisors such that their sum
# is coprime with N
import math as m
 
 
# Function to count all valid pairs
def CountPairs(n):
     
    # initialize count
    cnt = 0
     
    i = 1
     
    while i * i <= n :
         
        if(n % i == 0):
             
            div1 = i
            div2 = n//i
             
            sum = div1 + div2;
             
            # Check if sum of pair
            # and n are coprime
            if( m.gcd(sum, n) == 1):
                cnt += 1
         
        i += 1
     
    # Return the result
    return cnt
     
 
# Driver code
n = 24
 
print(CountPairs(n))


C#
// C# program to count all pairs of
// divisors such that their sum
// is coprime with N
using System;
 
class GFG {
 
    // Function to find gcd of a and b
    static int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    // Function to count all valid pairs
    static int CountPairs(int n)
    {
 
        // Initialize count
        int cnt = 0;
 
        for (int i = 1; i * i <= n; i++) {
            if (n % i == 0) {
                int div1 = i;
 
                int div2 = n / i;
 
                int sum = div1 + div2;
 
                // Check if sum of pair
                // and n are coprime
                if (gcd(sum, n) == 1)
                    cnt += 1;
            }
        }
        // Return the result
        return cnt;
    }
 
    // Driver method
    public static void Main()
    {
        int n = 24;
 
        Console.WriteLine(CountPairs(n));
    }
}


Javascript


输出:
2

时间复杂度: O(√N* log(N))