Count of distinct power of prime factor of N
Last Updated :
07 Apr, 2021
Given a positive integer N, the task is to find the total number of distinct power of prime factor of the given number N.
Examples:
Input: N = 216
Output: 4
Explanation:
216 can be expressed as 2 * 22 * 3 * 32.
The factors satisfying the conditions are 2, 22, 3 and 32 as all of them are written as distinct positive powers of prime factors.
Input: N = 24
Output: 3
Explanation:
24 can be expressed as 2 * 22 * 3
Approach: The idea is to find all the prime factors of N and how many times each prime factor divides N.
Suppose the prime factor ‘p’ divides N ‘z’ times, then the required distinct prime factors are p, p2, …, pi.
To find the number of distinct primes factor for prime number p find the minimum value of i such that (1 + 2 + …. + i) ? z.
Therefore, for each prime number dividing N K number of times, find the minimum value of i such that (1 + 2 + …. + i) ? K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countFac( int n)
{
int m = n;
int count = 0;
for ( int i = 2; (i * i) <= m; ++i) {
int total = 0;
while (n % i == 0) {
n /= i;
++total;
}
int temp = 0;
for ( int j = 1;
(temp + j) <= total;
++j) {
temp += j;
++count;
}
}
if (n != 1)
++count;
return count;
}
int main()
{
int N = 24;
cout << countFac(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countFac( int n)
{
int m = n;
int count = 0 ;
for ( int i = 2 ; (i * i) <= m; ++i)
{
int total = 0 ;
while (n % i == 0 )
{
n /= i;
++total;
}
int temp = 0 ;
for ( int j = 1 ; (temp + j) <= total; ++j)
{
temp += j;
++count;
}
}
if (n != 1 )
++count;
return count;
}
public static void main(String[] args)
{
int N = 24 ;
System.out.println(countFac(N));
}
}
|
Python3
def countFac(n):
m = n
count = 0
i = 2
while ((i * i) < = m):
total = 0
while (n % i = = 0 ):
n / = i
total + = 1
temp = 0
j = 1
while ((temp + j) < = total):
temp + = j
count + = 1
j + = 1
i + = 1
if (n ! = 1 ):
count + = 1
return count
N = 24
print (countFac(N))
|
C#
using System;
class GFG{
static int countFac( int n)
{
int m = n;
int count = 0;
for ( int i = 2; (i * i) <= m; ++i)
{
int total = 0;
while (n % i == 0)
{
n /= i;
++total;
}
int temp = 0;
for ( int j = 1; (temp + j) <= total; ++j)
{
temp += j;
++count;
}
}
if (n != 1)
++count;
return count;
}
public static void Main()
{
int N = 24;
Console.Write(countFac(N));
}
}
|
Javascript
<script>
function countFac(n)
{
var m = n;
var count = 0;
for ( var i = 2; (i * i) <= m; ++i)
{
var total = 0;
while (n % i == 0)
{
n /= i;
++total;
}
var temp = 0;
for ( var j = 1; (temp + j) <= total; ++j)
{
temp += j;
++count;
}
}
if (n != 1)
++count;
return count;
}
var N = 24;
document.write(countFac(N));
</script>
|
Time complexity: O(sqrt(N))
Share your thoughts in the comments
Please Login to comment...