📌  相关文章
📜  一个数的完全平方因数

📅  最后修改于: 2021-10-27 03:19:39             🧑  作者: Mango

给定一个整数N ,任务是找到N的完全平方因数。

例子:

朴素的方法:解决这个问题的最简单的方法是找到给定数字N 的所有可能的因子,并对每个因子,检查该因子是否是一个完美的平方。对于发现的每个因素,增加计数。打印最终计数
时间复杂度: 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


Javascript


输出:
4

时间复杂度: O(\sqrt{N})
空间复杂度: O(1)