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:
- 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.
- 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:
- Store all prime numbers ≤ N / 2, using Sieve of Eratosthenes in a vector, say prime.
- Store the number of primes in the range [3, N/2] in a variable, say x.
- 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++
#include <bits/stdc++.h>
using namespace std;
void primeFactorization( int N)
{
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;
}
}
}
vector< int > prime;
for ( int i = 3; i <= N / 2; i++)
if (sieve[i] == 0)
prime.push_back(i);
int x = prime.size();
cout << "2->" << x << endl;
for ( int i : prime)
cout << i << "->1" << endl;
}
int main()
{
int N = 18;
primeFactorization(N);
return 0;
}
|
Java
import java.util.*;
import java.util.HashMap;
class GFG{
static void primeFactorization( int N)
{
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 ;
}
}
}
ArrayList<Integer> prime = new ArrayList<Integer>();
for ( int i = 3 ; i <= N / 2 ; i++)
if (sieve[i] == 0 )
prime.add(i);
int x = prime.size();
System.out.println( "2->" + x);
for ( int i : prime)
System.out.println(i + "->1" );
}
public static void main(String args[])
{
int N = 18 ;
primeFactorization(N);
}
}
|
Python3
def primeFactorization(N):
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
prime = []
for i in range ( 3 , N / / 2 + 1 , 1 ):
if (sieve[i] = = 0 ):
prime.append(i)
x = len (prime)
print ( "2->" , x)
for i in prime:
print (i, "->1" )
if __name__ = = '__main__' :
N = 18
primeFactorization(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void primeFactorization( int N)
{
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;
}
}
}
List< int > prime = new List< int >();
for ( int i = 3; i <= N / 2; i++)
if (sieve[i] == 0)
prime.Add(i);
int x = prime.Count;
Console.WriteLine( "2->" + x);
foreach ( int i in prime)
Console.WriteLine(i + "->1" );
}
public static void Main(String[] args)
{
int N = 18;
primeFactorization(N);
}
}
|
Javascript
<script>
function primeFactorization(N)
{
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;
}
}
}
let prime = [];
for (let i = 3; i <= N / 2; i++)
if (sieve[i] == 0)
prime.push(i);
let x = prime.length;
document.write( "2->" + x);
document.write( "<br>" )
for (let i of prime)
{
document.write(i + "->1" );
document.write( "<br>" );
}
}
let N = 18;
primeFactorization(N);
</script>
|
Output2->3
3->1
5->1
7->1
Time Complexity: O(N * log(log(N)))
Auxiliary Space: O(N)