Dado un número N (N > 6) , la tarea es imprimir la descomposición en factores primos de un número Z , donde Z es el producto de todos los números ≤ N que son pares y se pueden expresar como el producto de dos números primos distintos.
Ejemplo:
Entrada: N = 6
Salida: 2→1
3→1
Explicación: 6 es el único número ≤ N, que es par y producto de dos números primos distintos (2 y 3). Por lo tanto, Z =6.
Ahora, factorización prima de Z =2×3Entrada: N = 5
Salida: 2→2
3→1
5→1
Explicación: Los únicos números pares ≤ N , que se pueden expresar como el producto de dos números primos distintos, son 6 (2×3) y 10 (2× 5). Por lo tanto, Z = 6*10=60 = 2x2x3x5
Observación: La siguiente observación ayuda a resolver el problema:
- Dado que los números requeridos deben ser pares y producto de dos números primos distintos , tendrán la forma 2×P , donde P es un número primo ≤ N / 2.
- Así, la factorización prima de Z será de la forma 2 x .3 1 .5 1 …P 1 , donde P es el último número primo ≤ N/2 y X es el número de números primos en el rango [3, N / 2].
Enfoque: Siga los pasos para resolver el problema:
- Almacene todos los números primos ≤ N / 2 , usando la criba de Eratóstenes en un vector, digamos primo .
- Almacene el número de primos en el rango [3, N/2] en una variable, digamos x .
- Imprima la descomposición en factores primos, donde el coeficiente de 2 es x y los coeficientes de todos los demás números primos en el rango [3, N/2] es 1.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the prime factorization of the product // of all numbers <=N that are even and can be expressed as a // product of two distinct prime numbers void primeFactorization(int N) { // sieve of Eratosthenese int sieve[N / 2 + 1] = { 0 }; for (int i = 2; i <= N / 2; i++) { if (sieve[i] == 0) { for (int j = i * i; j <= N / 2; j += i) { sieve[j] = 1; } } } // Store prime numbers in the range [3, N/2] vector<int> prime; for (int i = 3; i <= N / 2; i++) if (sieve[i] == 0) prime.push_back(i); // print the coefficient of 2 in the prime // factorization int x = prime.size(); cout << "2->" << x << endl; // print the coefficients of other primes for (int i : prime) cout << i << "->1" << endl; } // Driver code int main() { // Input int N = 18; // Function calling primeFactorization(N); return 0; }
Java
// Java implementation of // the above approach import java.util.*; import java.util.HashMap; class GFG{ // Function to print the prime factorization // of the product of all numbers <=N that are // even and can be expressed as a product of // two distinct prime numbers static void primeFactorization(int N) { // Sieve of Eratosthenese int[] sieve = new int[N / 2 + 1]; for(int i = 0; i <= N / 2; i++) { sieve[i] = 0; } for(int i = 2; i <= N / 2; i++) { if (sieve[i] == 0) { for(int j = i * i; j <= N / 2; j += i) { sieve[j] = 1; } } } // Store prime numbers in the range [3, N/2] ArrayList<Integer> prime = new ArrayList<Integer>(); for(int i = 3; i <= N / 2; i++) if (sieve[i] == 0) prime.add(i); // Print the coefficient of 2 in the prime // factorization int x = prime.size(); System.out.println("2->" + x); // Print the coefficients of other primes for(int i : prime) System.out.println(i + "->1"); } // Driver Code public static void main(String args[]) { // Input int N = 18; // Function calling primeFactorization(N); } } // This code is contributed by sanjoy_62
Python3
# Python3 implementation for the above approach # Function to print the prime factorization # of the product of all numbers <=N that are # even and can be expressed as a product of # two distinct prime numbers def primeFactorization(N): # Sieve of Eratosthenese sieve = [0 for i in range(N // 2 + 1)] for i in range(2, N // 2 + 1, 1): if (sieve[i] == 0): for j in range(i * i, N // 2 + 1, i): sieve[j] = 1 # Store prime numbers in the range [3, N/2] prime = [] for i in range(3, N // 2 + 1, 1): if (sieve[i] == 0): prime.append(i) # Print the coefficient of 2 in the # prime factorization x = len(prime) print("2->", x) # Print the coefficients of other primes for i in prime: print(i, "->1") # Driver code if __name__ == '__main__': # Input N = 18 # Function calling primeFactorization(N) # This code is contributed by ipg2016107
C#
// C# implementation of // the above approach using System; using System.Collections.Generic; class GFG{ // Function to print the prime factorization // of the product of all numbers <=N that are // even and can be expressed as a product of // two distinct prime numbers static void primeFactorization(int N) { // sieve of Eratosthenese int[] sieve = new int[N / 2 + 1]; for(int i = 0; i <= N / 2; i++) { sieve[i] = 0; } for(int i = 2; i <= N / 2; i++) { if (sieve[i] == 0) { for (int j = i * i; j <= N / 2; j += i) { sieve[j] = 1; } } } // Store prime numbers in the range [3, N/2] List<int> prime = new List<int>(); for(int i = 3; i <= N / 2; i++) if (sieve[i] == 0) prime.Add(i); // Print the coefficient of 2 in the prime // factorization int x = prime.Count; Console.WriteLine("2->" + x); // Print the coefficients of other primes foreach(int i in prime) Console.WriteLine(i + "->1"); } // Driver Code public static void Main(String[] args) { // Input int N = 18; // Function calling primeFactorization(N); } } // This code is contributed by avijitmondal1998
Javascript
<script> // JavaScript program for the above approach // Function to print the prime factorization // of the product of all numbers <=N that are // even and can be expressed as a product of // two distinct prime numbers function primeFactorization(N) { // Sieve of Eratosthenese let sieve = new Array(parseInt(N / 2) + 1).fill(0); for(let i = 2; i <= N / 2; i++) { if (sieve[i] == 0) { for(let j = i * i; j <= N / 2; j += i) { sieve[j] = 1; } } } // Store prime numbers in the range [3, N/2] let prime = []; for(let i = 3; i <= N / 2; i++) if (sieve[i] == 0) prime.push(i); // Print the coefficient of 2 in the prime // factorization let x = prime.length; document.write("2->" + x); document.write("<br>") // Print the coefficients of other primes for(let i of prime) { document.write(i + "->1"); document.write("<br>"); } } // Driver code // Input let N = 18; // Function calling primeFactorization(N); // This code is contributed by Potta Lokesh </script>
2->3 3->1 5->1 7->1
Complejidad de tiempo: O(N * log(log(N)))
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por anmolsingh1899 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA