Count of multiplicative partitions of N
Last Updated :
16 Jul, 2021
Given an integer N, the task is to find the total number of multiplicative partitions for N.
Multiplicative Partition: Number of ways of factoring of an integer with all factors greater than 1.
Examples:
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
Approach: The idea is to try for every divisor of the N and then recursively break the dividend to get the multiplicative partitions. Below are the illustrations of the steps of the approach:
- Initialize minimum factor as 2. Since it is the minimum factor other than 1.
- Start a loop from i = minimum to N – 1, and check if the number divides N and N/i > i, then increment the counter by 1 and again call the same function. Since i divides n so it means i and N/i can be factorized some more times.
For Example:
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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
static int getDivisors( int min, int n)
{
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;
}
int main()
{
int n = 30;
cout << 1 + getDivisors(2, n);
return 0;
}
|
Java
class MultiPart {
static int getDivisors( int min, int n)
{
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;
}
public static void main(String[] args)
{
int n = 30 ;
System.out.println(
1 + getDivisors( 2 , n));
}
}
|
Python3
def getDivisors( min , n):
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
if __name__ = = '__main__' :
n = 30
print ( 1 + getDivisors( 2 , n))
|
C#
using System;
class GFG{
static int getDivisors( int min, int n)
{
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;
}
public static void Main()
{
int n = 30;
Console.Write(1 + getDivisors(2, n));
}
}
|
Javascript
<script>
function getDivisors(min, n)
{
var total = 0;
for ( var i = min; i < n; ++i)
{
if (n % i == 0 && n / i >= i)
{
++total;
if (n / i > i)
total += getDivisors(i, n / i);
}
}
return total;
}
var n = 30;
document.write( 1 + getDivisors(2, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...