Dado un número N , la tarea es encontrar el valor mínimo de N aplicando las siguientes operaciones cualquier número de veces:
- Multiplique N por cualquier número entero positivo
- Reemplace N con sqrt(N) , solo si N es un cuadrado perfecto .
Ejemplos:
Entrada: N = 20
Salida: 10
Explicación:
Multiplicar -> 20 * 5 = 100
sqrt(100) = 10, que es el valor mínimo que se puede obtener.Entrada: N = 5184
Salida: 6
Explicación:
sqrt(5184) = 72.
Multiplica -> 72*18 = 1296
sqrt(1296) = 6, que es el valor mínimo que se puede obtener.
Enfoque : este problema se puede resolver utilizando el enfoque codicioso . A continuación se muestran los pasos:
- Siga reemplazando N por sqrt(N) hasta que N sea un cuadrado perfecto.
- Después del paso anterior, itere de sqrt(N) a 2 , y para cada, sigo reemplazando N con N / i si N es divisible por i 2 .
- El valor de N después del paso anterior será el valor mínimo posible.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to reduce N to its minimum // possible value by the given operations void minValue(int n) { // Keep replacing n until is // an integer while (int(sqrt(n)) == sqrt(n) && n > 1) { n = sqrt(n); } // Keep replacing n until n // is divisible by i * i for (int i = sqrt(n); i > 1; i--) { while (n % (i * i) == 0) n /= i; } // Print the answer cout << n; } // Driver Code int main() { // Given N int N = 20; // Function Call minValue(N); }
Java
// Java implementation of the above approach import java.lang.Math; class GFG{ // Function to reduce N to its minimum // possible value by the given operations static void minValue(int n) { // Keep replacing n until is // an integer while ((int)Math.sqrt(n) == Math.sqrt(n) && n > 1) { n = (int)(Math.sqrt(n)); } // Keep replacing n until n // is divisible by i * i for(int i = (int)(Math.sqrt(n)); i > 1; i--) { while (n % (i * i) == 0) n /= i; } // Print the answer System.out.println(n); } // Driver code public static void main(String args[]) { // Given N int N = 20; // Function call minValue(N); } } // This code is contributed by vikas_g
Python3
# Python3 program for the above approach import math # Function to reduce N to its minimum # possible value by the given operations def MinValue(n): # Keep replacing n until is # an integer while(int(math.sqrt(n)) == math.sqrt(n) and n > 1): n = math.sqrt(n) # Keep replacing n until n # is divisible by i * i for i in range(int(math.sqrt(n)), 1, -1): while (n % (i * i) == 0): n /= i # Print the answer print(n) # Driver code n = 20 # Function call MinValue(n) # This code is contributed by virusbuddah_
C#
// C# implementation of the approach using System; class GFG{ // Function to reduce N to its minimum // possible value by the given operations static void minValue(int n) { // Keep replacing n until is // an integer while ((int)Math.Sqrt(n) == Math.Sqrt(n) && n > 1) { n = (int)(Math.Sqrt(n)); } // Keep replacing n until n // is divisible by i * i for (int i = (int)(Math.Sqrt(n)); i > 1; i--) { while (n % (i * i) == 0) n /= i; } // Print the answer Console.Write(n); } // Driver code public static void Main() { // Given N int N = 20; // Function call minValue(N); } } // This code is contributed by vikas_g
Javascript
<script> // Javascript program for above approach // Function to reduce N to its minimum // possible value by the given operations function minValue(n) { // Keep replacing n until is // an integer while (parseInt(Math.sqrt(n)) == Math.sqrt(n) && n > 1) { n = parseInt(Math.sqrt(n)); } // Keep replacing n until n // is divisible by i * i for (var i = parseInt(Math.sqrt(n)); i > 1; i--) { while (n % (i * i) == 0) n /= i; } // Print the answer document.write(n); } // Driver Code // Given N var N = 20; // Function Call minValue(N); // This code is contributed by rutvik_56. </script>
Producción:
10
Complejidad temporal: O(N)
Espacio auxiliar: O(1)