Dado un entero positivo N , la tarea es encontrar el número más grande en el rango [1, N] tal que la raíz cuadrada del número sea menor que su factor primo más grande .
Entrada: N = 15
Salida: 15
Explicación: Los factores primos de 15 son {3, 5}. La raíz cuadrada de 15 es 3,87 (es decir, 3,87 < 5). Por lo tanto, 15 es el entero válido más grande en el rango dado.Entrada: N = 25
Salida: 23
Enfoque: El problema dado se puede resolver usando el tamiz de Eratóstenes con algunas modificaciones. Cree una array gpf[] , que almacene el mayor factor primo de todos los enteros en el rango dado. Inicialmente, gpf[] = {0} . Usando Sieve, inicialice todos los índices de la array gpf[] con el mayor factor primo del índice respectivo similar al algoritmo discutido en este artículo .
Ahora, itere sobre el rango [N, 1] de manera inversa e imprima el primer entero cuya raíz cuadrada del número sea menor que su mayor factor primo .
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; const int maxn = 100001; // Stores the Greatest Prime Factor int gpf[maxn]; // Modified Sieve to find the Greatest // Prime Factor of all integers in the // range [1, maxn] void modifiedSieve() { // Initialize the array with 0 memset(gpf, 0, sizeof(gpf)); gpf[0] = 0; gpf[1] = 1; // Iterate through all values of i for (int i = 2; i < maxn; i++) { // If i is not a prime number if (gpf[i] > 0) continue; // Update the multiples of i for (int j = i; j < maxn; j += i) { gpf[j] = max(i, gpf[j]); } } } // Function to find integer in the range // [1, N] such that its Greatest Prime // factor is greater than its square root int greatestValidInt(int N) { modifiedSieve(); // Iterate through all values of // i in the range [N, 1] for (int i = N; i > 0; i--) { // If greatest prime factor of i // is greater than its square root if (gpf[i] > sqrt(i)) { // Return answer return i; } } // If no valid integer exist return -1; } // Driver Code int main() { int N = 25; cout << greatestValidInt(N); return 0; }
Java
// Java program for the above approach public class GFG { final static int maxn = 100001; // Stores the Greatest Prime Factor static int gpf[] = new int[maxn]; // Modified Sieve to find the Greatest // Prime Factor of all integers in the // range [1, maxn] static void modifiedSieve() { // Initialize the array with 0 for (int i = 0; i < maxn; i++ ) gpf[i] = 0; gpf[0] = 0; gpf[1] = 1; // Iterate through all values of i for (int i = 2; i < maxn; i++) { // If i is not a prime number if (gpf[i] > 0) continue; // Update the multiples of i for (int j = i; j < maxn; j += i) { gpf[j] = Math.max(i, gpf[j]); } } } // Function to find integer in the range // [1, N] such that its Greatest Prime // factor is greater than its square root static int greatestValidInt(int N) { modifiedSieve(); // Iterate through all values of // i in the range [N, 1] for (int i = N; i > 0; i--) { // If greatest prime factor of i // is greater than its square root if (gpf[i] > Math.sqrt(i)) { // Return answer return i; } } // If no valid integer exist return -1; } // Driver Code public static void main (String[] args) { int N = 25; System.out.println(greatestValidInt(N)); } } // This code is contributed by AnkThon
Python3
# python program for the above approach import math maxn = 100001 # Stores the Greatest Prime Factor gpf = [0 for _ in range(maxn)] # Modified Sieve to find the Greatest # Prime Factor of all integers in the # range [1, maxn] def modifiedSieve(): # Initialize the array with 0 gpf[0] = 0 gpf[1] = 1 # Iterate through all values of i for i in range(2, maxn): # If i is not a prime number if (gpf[i] > 0): continue # Update the multiples of i for j in range(i, maxn, i): gpf[j] = max(i, gpf[j]) # Function to find integer in the range # [1, N] such that its Greatest Prime # factor is greater than its square root def greatestValidInt(N): modifiedSieve() # Iterate through all values of # i in the range [N, 1] for i in range(N, 0, -1): # If greatest prime factor of i # is greater than its square root if (gpf[i] > math.sqrt(i)): # Return answer return i # If no valid integer exist return -1 # Driver Code if __name__ == "__main__": N = 25 print(greatestValidInt(N)) # This code is contributed by rakeshsahni
C#
// C# program for the above approach using System; public class GFG { static int maxn = 100001; // Stores the Greatest Prime Factor static int[] gpf = new int[maxn]; // Modified Sieve to find the Greatest // Prime Factor of all integers in the // range [1, maxn] static void modifiedSieve() { // Initialize the array with 0 for (int i = 0; i < maxn; i++) gpf[i] = 0; gpf[0] = 0; gpf[1] = 1; // Iterate through all values of i for (int i = 2; i < maxn; i++) { // If i is not a prime number if (gpf[i] > 0) continue; // Update the multiples of i for (int j = i; j < maxn; j += i) { gpf[j] = Math.Max(i, gpf[j]); } } } // Function to find integer in the range // [1, N] such that its Greatest Prime // factor is greater than its square root static int greatestValidInt(int N) { modifiedSieve(); // Iterate through all values of // i in the range [N, 1] for (int i = N; i > 0; i--) { // If greatest prime factor of i // is greater than its square root if (gpf[i] > Math.Sqrt(i)) { // Return answer return i; } } // If no valid integer exist return -1; } // Driver Code public static void Main(string[] args) { int N = 25; Console.WriteLine(greatestValidInt(N)); } } // This code is contributed by ukasp.
Javascript
<script> // Javascript program for the above approach const maxn = 100001; // Stores the Greatest Prime Factor let gpf = new Array(maxn); // Modified Sieve to find the Greatest // Prime Factor of all integers in the // range [1, maxn] function modifiedSieve() { // Initialize the array with 0 gpf.fill(0); gpf[0] = 0; gpf[1] = 1; // Iterate through all values of i for (let i = 2; i < maxn; i++) { // If i is not a prime number if (gpf[i] > 0) continue; // Update the multiples of i for (let j = i; j < maxn; j += i) { gpf[j] = Math.max(i, gpf[j]); } } } // Function to find integer in the range // [1, N] such that its Greatest Prime // factor is greater than its square root function greatestValidInt(N) { modifiedSieve(); // Iterate through all values of // i in the range [N, 1] for (let i = N; i > 0; i--) { // If greatest prime factor of i // is greater than its square root if (gpf[i] > Math.sqrt(i)) { // Return answer return i; } } // If no valid integer exist return -1; } // Driver Code let N = 25; document.write(greatestValidInt(N)); // This code is contributed by gfgking. </script>
23
Complejidad de tiempo: O(N*log N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por pratik0381200 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA