📜  数的完美平方因数

📅  最后修改于: 2021-04-26 09:21:05             🧑  作者: Mango

给定整数N ,任务是找到N的因子的数量,它们是理想平方。
例子:

天真的方法:解决此问题的最简单方法是找到给定数N的所有可能因子,并针对每个因子检查该因子是否为理想平方。对于发现的每个因素,增加count 。打印最终计数
时间复杂度: O(N)
辅助空间: O(1)

高效方法:
需要进行以下观察以优化上述方法:
一个数字的因数由下式给出:

在一个完美的平方中,不同素数的数量必须被2整除。因此,作为一个完美平方的因数的数量由下式给出:

插图:

因此,找到素数的计数并应用上面的公式来找到一个完美平方的因数。
下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function that returns the count of
// factors that are perfect squares
int noOfFactors(int N)
{
    if (N == 1)
        return 1;
  
    // Stores the count of number
    // of times a prime number
    // divides N.
    int count = 0;
  
    // Stores the number of factors
    // that are perfect square
    int ans = 1;
  
    // Count number of 2's
    // that divides N
    while (N % 2 == 0) {
        count++;
        N = N / 2;
    }
  
    // Calculate ans according
    // to above formula
    ans *= (count / 2 + 1);
  
    // Check for all the possible
    // numbers that can divide it
    for (int i = 3;
         i * i <= N; i = i + 2) {
        count = 0;
  
        // Check the number of
        // times prime number
        // i divides it
        while (N % i == 0) {
            count++;
            N = N / i;
        }
  
        // Calculate ans according
        // to above formula
        ans *= (count / 2 + 1);
    }
  
    // Return final count
    return ans;
}
  
// Driver Code
int main()
{
    int N = 100;
  
    cout << noOfFactors(N);
  
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
  
class GFG{
  
// Function that returns the count of
// factors that are perfect squares
static int noOfFactors(int N)
{
    if (N == 1)
        return 1;
  
    // Stores the count of number
    // of times a prime number
    // divides N.
    int count = 0;
  
    // Stores the number of factors
    // that are perfect square
    int ans = 1;
  
    // Count number of 2's
    // that divides N
    while (N % 2 == 0) 
    {
        count++;
        N = N / 2;
    }
  
    // Calculate ans according
    // to above formula
    ans *= (count / 2 + 1);
  
    // Check for all the possible
    // numbers that can divide it
    for(int i = 3; i * i <= N; i = i + 2) 
    {
        count = 0;
  
        // Check the number of
        // times prime number
        // i divides it
        while (N % i == 0)
        {
            count++;
            N = N / i;
        }
  
        // Calculate ans according
        // to above formula
        ans *= (count / 2 + 1);
    }
  
    // Return final count
    return ans;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 100;
  
    System.out.print(noOfFactors(N));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
  
# Function that returns the count of
# factors that are perfect squares
def noOfFactors(N):
  
    if (N == 1):
        return 1
  
    # Stores the count of number
    # of times a prime number
    # divides N.
    count = 0
  
    # Stores the number of factors
    # that are perfect square
    ans = 1
  
    # Count number of 2's
    # that divides N
    while (N % 2 == 0):
        count += 1
        N = N // 2
  
    # Calculate ans according
    # to above formula
    ans *= (count // 2 + 1)
  
    # Check for all the possible
    # numbers that can divide it
    i = 3
    while i * i <= N:
        count = 0
  
        # Check the number of
        # times prime number
        # i divides it
        while (N % i == 0):
            count += 1
            N = N // i
  
        # Calculate ans according
        # to above formula
        ans *= (count // 2 + 1)
        i += 2
      
    # Return final count
    return ans
  
# Driver Code
if __name__ == "__main__":
      
    N = 100
  
    print(noOfFactors(N))
  
# This code is contributed by chitranayal


C#
// C# program to implement
// the above approach
using System;
  
class GFG{
  
// Function that returns the count of
// factors that are perfect squares
static int noOfFactors(int N)
{
    if (N == 1)
        return 1;
  
    // Stores the count of number
    // of times a prime number
    // divides N.
    int count = 0;
  
    // Stores the number of factors
    // that are perfect square
    int ans = 1;
  
    // Count number of 2's
    // that divides N
    while (N % 2 == 0) 
    {
        count++;
        N = N / 2;
    }
  
    // Calculate ans according
    // to above formula
    ans *= (count / 2 + 1);
  
    // Check for all the possible
    // numbers that can divide it
    for(int i = 3; i * i <= N; i = i + 2) 
    {
        count = 0;
  
        // Check the number of
        // times prime number
        // i divides it
        while (N % i == 0)
        {
            count++;
            N = N / i;
        }
  
        // Calculate ans according
        // to above formula
        ans *= (count / 2 + 1);
    }
  
    // Return final count
    return ans;
}
  
// Driver Code
public static void Main(String[] args)
{
    int N = 100;
  
    Console.Write(noOfFactors(N));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
4

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