给定一个整数N ,任务是找到N的完全平方因数。
例子:
Input: N = 100
Output: 4
Explanation:
There are four factors of
100 (1, 4, 25, 100) that are perfect square.
Input: N = 900
Output: 8
Explanation:
There are eight factors of 900 (1, 4, 9, 25, 36, 100, 225, 900) that are perfect square.
朴素的方法:解决这个问题的最简单的方法是找到给定数字N 的所有可能的因子,并对每个因子,检查该因子是否是一个完美的平方。对于发现的每个因素,增加计数。打印最终计数。
时间复杂度: O(N)
辅助空间: O(1)
有效的方法:
为了优化上述方法,需要进行以下观察:
一个数的因数数由下式给出:
Factors of N = (1 + a1)*(1 + a2)*(1 + a3)*..*(1 + an)
where a1, a2, a3, …, an are the count of distinct prime factors of N.
在完全平方数中,不同质因数的个数必须能被 2 整除。 因此,完全平方数的因数由下式给出:
Factors of N that are perfect square = (1 + a1/2)*(1 + a2/2)*…*(1 + an/2) where a1, a2, a3, …, an are the count of distinct prime factors of N.
插图:
The prime factors of N = 100 are 2, 2, 5, 5.
Therefore, the number of factors that are perfect square are (1 + 2/2) * (1 + 2/2) = 4.
The factors are 1, 4, 25, 100.
因此,求质因数的个数并应用上述公式求出完全平方数的因数。
下面是上述方法的实现:
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(1)