# Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers

Given a number N (N > 6), the task is to print the prime factorization of a number Z, where Z is the product of all numbers â‰¤ N that are even and can be expressed as the product of two distinct prime numbers.

Example:

Input: N = 6
Output: 2â†’1
3â†’1
Explanation: 6 is the only number â‰¤ N, which is even and a product of two distinct prime numbers (2 and 3). Therefore, Z=6.
Now, prime factorization of Z=2Ã—3

Input: N = 5
Output: 2â†’2
3â†’1
5â†’1
Explanation: The only even numbers â‰¤N, which can be expressed as the product of two distinct prime numbers, are 6 (2×3) and 10 (2×5). Therefore, Z = 6*10=60 = 2x2x3x5

Observation: The following observation helps to solve the problem:

1. Since the required numbers need to be even and product of two distinct prime numbers, they will be of the form 2Ã—P, where P is a prime number â‰¤ N / 2.
2. Thus, the prime factorization of Z will be of the form 2x.31.51…P1, where P is the last prime number â‰¤ N/2 and X is the number of prime numbers in the range [3, N / 2].

Approach: Follow the steps to solve the problem:

1. Store all prime numbers â‰¤ N / 2, using Sieve of Eratosthenes in a vector, say prime.
2. Store the number of primes in the range [3,  N/2] in a variable, say x.
3. Print the prime factorization, where the coefficient of 2 is x and the coefficients of all other primes in the range [3, N/2] is 1.

Below is the implementation of the above approach:

## 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  Eratosthenes` `    ``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  Eratosthenes` `    ``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  Eratosthenes` `    ``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  Eratosthenes` `    ``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

 ``

Output

```2->3
3->1
5->1
7->1```

Time Complexity: O(N * log(log(N)))
Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next
Similar Reads
Complete Tutorials