给定一个整数N,任务是找到一个完美立方体的N个因子。
例子:
Input: N = 27
Output: 2
Explanation :
There are 2 factors of 27 (1, 27) that are perfect cube
Input: N = 216
Output: 4
Explanation:
There are 4 factors of 216 (1, 8, 27, 216) that are perfect cube
幼稚的方法:幼稚的想法是找到给定数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.
在一个完美的多维数据集中,不同素数的数量必须被3整除。因此,作为一个完美的多维数据集的因子的数量由下式给出:
Factors of N that are perfect cube = (1 + a1/3)*(1 + a2/3)*…*(1 + an/3)
where a1, a2, a3, .., an are the count of distinct prime factors of N.
插图:
The factors of N = 216 are 2, 2, 2, 3, 3, 3.
Therefore, number of factors that are perfect cube are (1 + 3/3) * (1 + 3/3) = 4. The factors are 1, 8, 27, and 216.
因此,找到素因数并应用上面的公式来找到一个完美立方体的因数。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function that returns the count
// of factors that are perfect cube
int noOfFactors(int N)
{
if (N == 1)
return 1;
// To store the count of number
// of times a prime number
// divides N.
int count = 0;
// To store the number of factors
// that are perfect cube
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 / 3 + 1);
// Check for all the possible
// numbers that can divide it
for (int i = 3; i * i <= N; i = i + 2) {
count = 0;
// Loop to 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 / 3 + 1);
}
// Return final count
return ans;
}
// Driver Code
int main()
{
// Given number
int N = 216;
// Function Call
cout << noOfFactors(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function that returns the count
// of factors that are perfect cube
static int noOfFactors(int N)
{
if (N == 1)
return 1;
// To store the count of number
// of times a prime number
// divides N.
int count = 0;
// To store the number of factors
// that are perfect cube
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 / 3 + 1);
// Check for all the possible
// numbers that can divide it
for(int i = 3; i * i <= N; i = i + 2)
{
count = 0;
// Loop to 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 / 3 + 1);
}
// Return final count
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given number
int N = 216;
// Function call
System.out.print(noOfFactors(N));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function that returns the count
# of factors that are perfect cube
def noofFactors(N):
if N == 1:
return 1
# To store the count of number
# of times a prime number
# divides N
count = 0
# To store the count of factors that
# are perfect cube
ans = 1
# Count number of 2's that divides N
while(N % 2 == 0):
count += 1
N //= 2
# Calculate ans according
# to above formula
ans *= ((count // 3) + 1)
# Check for all possible
# numbers that can divide it
i = 3
while((i * i) <= N):
count = 0
# Loop to check the number
# of times prime number
# i divides it
while(N % i == 0):
count += 1
N //= i
# Calculate ans according
# to above formula
ans *= ((count // 3) + 1)
i += 2
return ans
# Driver Code
# Given number
N = 216
# Function call
print(noofFactors(N))
# This code is contributed by VirusBuddah_
C#
// C# program for the above approach
using System;
class GFG{
// Function that returns the count
// of factors that are perfect cube
static int noOfFactors(int N)
{
if (N == 1)
return 1;
// To store the count of number
// of times a prime number
// divides N.
int count = 0;
// To store the number of factors
// that are perfect cube
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 / 3 + 1);
// Check for all the possible
// numbers that can divide it
for(int i = 3; i * i <= N; i = i + 2)
{
count = 0;
// Loop to 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 / 3 + 1);
}
// Return final count
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Given number
int N = 216;
// Function call
Console.Write(noOfFactors(N));
}
}
// This code is contributed by Rajput-Ji
Javascript
4
时间复杂度: O(log(N))
辅助空间: O(1)