📜  给定数字的平方自由除数的计数

📅  最后修改于: 2021-04-27 17:49:31             🧑  作者: Mango

给定整数N ,任务是计算给定数字的无平方除数的数量。

例子:

天真的方法:
对于每个整数N ,找到其因数,然后检查它是否为无平方数。如果它是一个无平方的数字,则增加计数或否则转到下一个数字。最后,打印计数,得出我们所需的N的无平方除数。
时间复杂度: O(N 3/2 )

高效方法:
请按照以下步骤解决问题:

  • 根据无平方数的定义,可以理解,通过找出给定数N的所有素数,可以找出可以除N的所有可能的无平方数。
  • N的素数为X。因此,使用这些X质数可以形成2个X – 1个无平方数。
  • 由于这些X素数因子中的每一个都是N的因数,因此,这些X素数因子的任何乘积组合也是N的因子,因此将有2个X – 1平方的N除数。

下面是上述方法的实现:

C++
// C++ Program to find the square
// free divisors of a given number
#include 
using namespace std;
  
// The function to check
// if a number is prime or not
bool IsPrime(int i)
{
    // If the number is even
    // then its not prime
    if (i % 2 == 0 && i != 2)
        return false;
  
    else {
        for (int j = 3;
             j <= sqrt(i); j += 2) {
            if (i % j == 0)
                return false;
        }
        return true;
    }
}
  
// Driver Code
int main()
{
    // Stores the count of
    // distinct prime factors
    int c = 0;
    int N = 72;
  
    for (int i = 2;
         i <= sqrt(N); i++) {
  
        if (IsPrime(i)) {
            if (N % i == 0) {
                c++;
                if (IsPrime(N / i)
                    && i != (N / i)) {
                    c++;
                }
            }
        }
    }
  
    // Print the number of
    // square-free divisors
    cout << pow(2, c) - 1
         << endl;
    return 0;
}


Java
// Java program to find the square 
// free divisors of a given number
import java.util.*;
  
class GFG{
      
// The function to check 
// if a number is prime or not 
static boolean IsPrime(int i)
{
      
    // If the number is even 
    // then its not prime 
    if (i % 2 == 0 && i != 2) 
        return false; 
    else
    {
        for(int j = 3; 
                j <= Math.sqrt(i); 
                j += 2) 
        {
           if (i % j == 0)
               return false;
        }
        return true; 
    }
}
  
// Driver code 
public static void main(String[] args) 
{
      
    // Stores the count of 
    // distinct prime factors 
    int c = 0; 
    int N = 72;
      
    for(int i = 2; 
            i <= Math.sqrt(N); i++)
    {
       if (IsPrime(i))
       {
           if (N % i == 0)
           {
               c++; 
               if (IsPrime(N / i) && 
                     i != (N / i))
                   c++;
           }
       }
    }
      
    // Print the number of 
    // square-free divisors 
    System.out.print(Math.pow(2, c) - 1);
}
}
  
// This code is contributed by sanjoy_62


Python3
# Python3 program to find the square 
# free divisors of a given number 
import math
  
# The function to check 
# if a number is prime or not
def IsPrime(i):
      
    # If the number is even 
    # then its not prime 
    if (i % 2 == 0 and i != 2):
        return 0; 
          
    else:
        for j in range(3, int(math.sqrt(i) + 1), 2):
            if (i % j == 0):
                return 0;
                  
        return 1; 
  
# Driver code 
  
# Stores the count of 
# distinct prime factors
c = 0; 
N = 72;
  
for i in range(2, int(math.sqrt(N)) + 1):
    if (IsPrime(i)):
        if (N % i == 0):
            c = c + 1
  
            if (IsPrime(N / i) and 
                 i != (N / i)):
                c = c + 1
                  
# Print the number of 
# square-free divisors     
print (pow(2, c) - 1)
  
# This code is contributed by sanjoy_62


C#
// C# program to find the square 
// free divisors of a given number
using System;
class GFG{
      
// The function to check 
// if a number is prime or not 
static Boolean IsPrime(int i)
{
      
    // If the number is even 
    // then its not prime 
    if (i % 2 == 0 && i != 2) 
        return false; 
    else
    {
        for(int j = 3; 
                j <= Math.Sqrt(i); 
                j += 2) 
        {
        if (i % j == 0)
            return false;
        }
        return true; 
    }
}
  
// Driver code 
public static void Main(String[] args) 
{
      
    // Stores the count of 
    // distinct prime factors 
    int c = 0; 
    int N = 72;
      
    for(int i = 2; 
            i <= Math.Sqrt(N); i++)
    {
        if (IsPrime(i))
        {
            if (N % i == 0)
            {
                c++; 
                if (IsPrime(N / i) && 
                        i != (N / i))
                    c++;
            }
        }
    }
      
    // Print the number of 
    // square-free divisors 
    Console.Write(Math.Pow(2, c) - 1);
}
}
  
// This code is contributed by shivanisinghss2110


输出:
3

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