给定整数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的所有可能因子,并针对每个因子检查该因子是否为理想平方。对于发现的每个因素,增加count 。打印最终计数。
时间复杂度: 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
4
时间复杂度: O(log(N))
空间复杂度: O(1)