Dado un entero positivo N , la tarea es encontrar el número total de potencias distintas del factor primo del número dado N .
Ejemplos:
Entrada: N = 216
Salida: 4
Explicación:
216 se puede expresar como 2 * 2 2 * 3 * 3 2 .
Los factores que satisfacen las condiciones son 2, 2 2 , 3 y 3 2 ya que todos ellos se escriben como distintas potencias positivas de factores primos.
Entrada: N = 24
Salida: 3
Explicación:
24 se puede expresar como 2 * 2 2 * 3
Enfoque: La idea es encontrar todos los factores primos de N y cuántas veces cada factor primo divide a N .
Supongamos que el factor primo ‘p’ divide N ‘z’ veces, entonces los factores primos distintos requeridos son p, p 2 , …, p i .
Para encontrar el número de factores primos distintos para el número primo p, encuentre el valor mínimo de i tal que (1 + 2 + …. + i) ≤ z .
Por lo tanto, para cada número primo que divide N K número de veces, encuentre el valor mínimo de i tal que (1 + 2 + …. + i) ≤ K .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count the number // of distinct positive power of // prime factor of integer N int countFac(int n) { int m = n; int count = 0; // Iterate for all prime factor for (int i = 2; (i * i) <= m; ++i) { int total = 0; // If it is a prime factor, // count the total number // of times it divides n. while (n % i == 0) { n /= i; ++total; } int temp = 0; // Find the Number of distinct // possible positive numbers for (int j = 1; (temp + j) <= total; ++j) { temp += j; ++count; } } if (n != 1) ++count; // Return the final count return count; } // Driver Code int main() { // Given Number N int N = 24; // Function Call cout << countFac(N); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to count the number // of distinct positive power of // prime factor of integer N static int countFac(int n) { int m = n; int count = 0; // Iterate for all prime factor for(int i = 2; (i * i) <= m; ++i) { int total = 0; // If it is a prime factor, // count the total number // of times it divides n. while (n % i == 0) { n /= i; ++total; } int temp = 0; // Find the Number of distinct // possible positive numbers for(int j = 1; (temp + j) <= total; ++j) { temp += j; ++count; } } if (n != 1) ++count; // Return the final count return count; } // Driver code public static void main(String[] args) { // Given Number N int N = 24; // Function Call System.out.println(countFac(N)); } } // This code is contributed by Pratima Pandey
Python3
# Python3 program for the above approach # Function to count the number # of distinct positive power of # prime factor of integer N def countFac(n): m = n count = 0 # Iterate for all prime factor i = 2 while((i * i) <= m): total = 0 # If it is a prime factor, # count the total number # of times it divides n. while (n % i == 0): n /= i total += 1 temp = 0 # Find the Number of distinct # possible positive numbers j = 1 while((temp + j) <= total): temp += j count += 1 j += 1 i += 1 if (n != 1): count += 1 # Return the final count return count # Driver Code # Given number N N = 24 # Function call print(countFac(N)) # This code is contributed by sanjoy_62
C#
// C# program for the above approach using System; class GFG{ // Function to count the number // of distinct positive power of // prime factor of integer N static int countFac(int n) { int m = n; int count = 0; // Iterate for all prime factor for(int i = 2; (i * i) <= m; ++i) { int total = 0; // If it is a prime factor, // count the total number // of times it divides n. while (n % i == 0) { n /= i; ++total; } int temp = 0; // Find the Number of distinct // possible positive numbers for(int j = 1; (temp + j) <= total; ++j) { temp += j; ++count; } } if (n != 1) ++count; // Return the final count return count; } // Driver code public static void Main() { // Given Number N int N = 24; // Function Call Console.Write(countFac(N)); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript program for the above approach // Function to count the number // of distinct positive power of // prime factor of integer N function countFac(n) { var m = n; var count = 0; // Iterate for all prime factor for(var i = 2; (i * i) <= m; ++i) { var total = 0; // If it is a prime factor, // count the total number // of times it divides n. while (n % i == 0) { n /= i; ++total; } var temp = 0; // Find the Number of distinct // possible positive numbers for(var j = 1; (temp + j) <= total; ++j) { temp += j; ++count; } } if (n != 1) ++count; // Return the final count return count; } // Driver code // Given Number N var N = 24; // Function Call document.write(countFac(N)); // This code is contributed by Khushboogoyal499 </script>
3
Complejidad del tiempo: O(sqrt(N))
Publicación traducida automáticamente
Artículo escrito por shreyasshetty788 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA