📌  相关文章
📜  找到 Z 的质因子,使得 Z 是所有偶数的乘积,直到 N 是两个不同质数的乘积

📅  最后修改于: 2022-05-13 01:56:05.699000             🧑  作者: Mango

找到 Z 的质因子,使得 Z 是所有偶数的乘积,直到 N 是两个不同质数的乘积

给定一个数N (N > 6) ,任务是打印一个数Z的素数分解,其中Z是所有 ≤ N的偶数和 可以表示为两个不同素数的乘积。

例子:

观察:以下观察有助于解决问题:

  1. 由于所需的数字必须是偶数并且是两个不同质数的乘积,因此它们将采用2×P的形式,其中P 是质数≤ N / 2。
  2. 因此, Z的素数分解将是 2 x .3 1 .5 1 …P 1的形式,其中P是最后一个素≤ N/2X[3, N /2]。

方法:按照以下步骤解决问题:

  1. 使用 Eratosthenes 筛法将所有质数 ≤ N / 2存储在向量中,例如prime
  2. [3, N/2]范围内的素数数存储在变量中,例如x
  3. 打印素数分解,其中 2 的系数为x[3, N/2]范围内所有其他素数的系数为 1。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
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 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 prime = new ArrayList();
    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 prime = new List();
    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


输出
2->3
3->1
5->1
7->1

时间复杂度: O(N * log(log(N)))
辅助空间: O(N)