📜  打印所有 N 的除数且与除数商互质的数

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

打印所有 N 的除数且与除数商互质的数

给定一个正整数N ,任务是打印所有数字,比如K ,使得KN的除数,并且KN / K互质。

例子:

朴素方法:解决给定问题的最简单方法是迭代范围[1, N] 并检查每个数字K是否KN的除数 K 和 N/K 的 GCD 是否为1 。如果发现是true ,则打印K 。否则,检查下一个数字。

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

有效方法:上述方法也可以通过使用所有除数成对存在的观察来优化。例如,如果N100 ,那么所有的除数对是: (1, 100), (2, 50), (4, 25), (5, 20), (10, 10)

因此,想法是在[1, √N]范围内进行迭代并检查是否满足两个给定条件,即任何数K是否是N的除数 KN/K的 GCD 是否为1 。如果发现是true ,则打印两个数字。在两个相等的除数的情况下,只打印其中一个。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print all numbers
// that are divisors of N and are
// co-prime with the quotient
// of their division
void printUnitaryDivisors(int n)
{
    // Iterate upto square root of N
    for (int i = 1; i <= sqrt(n); i++) {
 
        if (n % i == 0) {
 
            // If divisors are equal and gcd is
            // 1, then print only one of them
            if (n / i == i && __gcd(i, n / i) == 1) {
                printf("%d ", i);
            }
 
            // Otherwise print both
            else {
                if (__gcd(i, n / i) == 1) {
                    printf("%d %d ", i, n / i);
                }
            }
        }
    }
}
 
// Driver Code
int main()
{
    int N = 12;
    printUnitaryDivisors(N);
 
    return 0;
}


Python3
# python 3 program for the above approach
from math import sqrt, gcd
 
# Function to print all numbers
# that are divisors of N and are
# co-prime with the quotient
# of their division
def printUnitaryDivisors(n):
   
    # Iterate upto square root of N
    for i in range(1,int(sqrt(n)) + 1, 1):
        if (n % i == 0):
           
            # If divisors are equal and gcd is
            # 1, then print only one of them
            if (n // i == i and gcd(i, n // i) == 1):
                print(i)
 
            # Otherwise print both
            else:
                if (gcd(i, n // i) == 1):
                    print(i, n // i,end = " ")
                 
# Driver Code
if __name__ == '__main__':
    N = 12
    printUnitaryDivisors(N)
 
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
  
static int gcd(int a, int b)
{
    return b == 0 ? a : gcd(b, a % b);
}
// Function to print all numbers
// that are divisors of N and are
// co-prime with the quotient
// of their division
static void printUnitaryDivisors(int n)
{
    // Iterate upto square root of N
    for (int i = 1; i <= (int)Math.Sqrt(n); i++) {
 
        if (n % i == 0) {
 
            // If divisors are equal and gcd is
            // 1, then print only one of them
            if (n / i == i && gcd(i, n / i) == 1) {
                Console.Write(i+" ");
            }
 
            // Otherwise print both
            else {
                if (gcd(i, n / i) == 1) {
                    Console.Write(i + " " +n / i+ " ");
                }
            }
        }
    }
}
 
// Driver Code
public static void Main()
{
    int N = 12;
    printUnitaryDivisors(N);
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    static int gcd(int a, int b)
    {
        return b == 0 ? a : gcd(b, a % b);
    }
    // Function to print all numbers
    // that are divisors of N and are
    // co-prime with the quotient
    // of their division
    static void printUnitaryDivisors(int n)
    {
        // Iterate upto square root of N
        for (int i = 1; i <= (int)Math.sqrt(n); i++) {
 
            if (n % i == 0) {
 
                // If divisors are equal and gcd is
                // 1, then print only one of them
                if (n / i == i && gcd(i, n / i) == 1) {
                    System.out.print(i + " ");
                }
 
                // Otherwise print both
                else {
                    if (gcd(i, n / i) == 1) {
                        System.out.print(i + " " + n / i
                                         + " ");
                    }
                }
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 12;
        printUnitaryDivisors(N);
    }
}


Javascript


输出:
1 12 3 4

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