给定整数N ,任务是找到N的乘法分区总数。
Multiplicative Partition: Number of ways of factoring of an integer with all factors greater than 1.
例子:
Input: N = 20
Output: 4
Explanation:
Multiplicative partitions of 20 are:
2 × 2 × 5 = 2 × 10 = 4 × 5 = 20.
Input: N = 30
Output: 5
Explanation:
Multiplicative partitions of 30 are:
2 × 3 × 5 = 2 × 15 = 6 × 5 = 3 × 10 = 30
方法:这个想法是尝试对N的每个除数进行尝试,然后递归地打破红利以得到乘法分区。以下是方法步骤的说明:
- 将最小因子初始化为2。因为它是除1以外的最小因子。
- 从i =最小值到N – 1开始循环,并检查数字是否除以N并且N / i> i,然后将计数器加1,然后再次调用同一函数。由于i除以n,因此意味着i和N / i可以被分解更多次。
例如:
If N = 30, let i = min = 2
30 % 2 = 0, so again recur with (2, 15)
15 % 3 = 0, so again recur with (3, 5)
.
.
and so on.
下面是上述方法的实现:
C++
// C++ implementation to find
// the multiplicative partitions of
// the given number N
#include
using namespace std;
// Function to return number of ways
// of factoring N with all
// factors greater than 1
static int getDivisors(int min, int n)
{
// Variable to store number of ways
// of factoring n with all
// factors greater than 1
int total = 0;
for(int i = min; i < n; ++i)
{
if (n % i == 0 && n / i >= i)
{
++total;
if (n / i > i)
total += getDivisors(i, n / i);
}
}
return total;
}
// Driver code
int main()
{
int n = 30;
// 2 is the minimum factor of
// number other than 1.
// So calling recursive
// function to find
// number of ways of factoring N
// with all factors greater than 1
cout << 1 + getDivisors(2, n);
return 0;
}
// This code is contributed by rutvik_56
Java
// Java implementation to find
// the multiplicative partitions of
// the given number N
class MultiPart {
// Function to return number of ways
// of factoring N with all
// factors greater than 1
static int getDivisors(int min, int n)
{
// Variable to store number of ways
// of factoring n with all
// factors greater than 1
int total = 0;
for (int i = min; i < n; ++i)
if (n % i == 0 && n / i >= i) {
++total;
if (n / i > i)
total
+= getDivisors(
i, n / i);
}
return total;
}
// Driver Code
public static void main(String[] args)
{
int n = 30;
// 2 is the minimum factor of
// number other than 1.
// So calling recursive
// function to find
// number of ways of factoring N
// with all factors greater than 1
System.out.println(
1 + getDivisors(2, n));
}
}
Python3
# Python3 implementation to find
# the multiplicative partitions of
# the given number N
# Function to return number of ways
# of factoring N with all
# factors greater than 1
def getDivisors(min, n):
# Variable to store number of ways
# of factoring n with all
# factors greater than 1
total = 0
for i in range(min, n):
if (n % i == 0 and n // i >= i):
total += 1
if (n // i > i):
total += getDivisors(i, n // i)
return total
# Driver code
if __name__ == '__main__':
n = 30
# 2 is the minimum factor of
# number other than 1.
# So calling recursive
# function to find
# number of ways of factoring N
# with all factors greater than 1
print(1 + getDivisors(2, n))
# This code is contributed by mohit kumar 29
C#
// C# implementation to find
// the multiplicative partitions of
// the given number N
using System;
class GFG{
// Function to return number of ways
// of factoring N with all
// factors greater than 1
static int getDivisors(int min, int n)
{
// Variable to store number of ways
// of factoring n with all
// factors greater than 1
int total = 0;
for(int i = min; i < n; ++i)
if (n % i == 0 && n / i >= i)
{
++total;
if (n / i > i)
total+= getDivisors(i, n / i);
}
return total;
}
// Driver code
public static void Main()
{
int n = 30;
// 2 is the minimum factor of
// number other than 1.
// So calling recursive
// function to find
// number of ways of factoring N
// with all factors greater than 1
Console.Write(1 + getDivisors(2, n));
}
}
// This code is contributed by adityakumar27200
输出:
5