Print all Semi-Prime Numbers less than or equal to N
Given an integer N, the task is to print all the semi-prime numbers ? N.
A semi-prime number is an integer that can be expressed as a product of two distinct prime numbers.
For example, 15 = 3 * 5 is a semi-prime number but 9 = 3 * 3 is not.
Examples:
Input: N = 20
Output: 6 10 14 15
Input: N = 50
Output: 6 10 14 15 21 22 26 33 34 35 38 39 46
Prerequisites:
Approach: For every number < N, count the number of prime factors it has. If the number of prime factors is 2 then the number is a semi-prime number as all the semi-prime numbers have only 2 prime factors.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > createSemiPrimeSieve( int n)
{
int v[n + 1];
for ( int i = 1; i <= n; i++)
v[i] = i;
int countDivision[n + 1];
for ( int i = 0; i < n + 1; i++)
countDivision[i] = 2;
for ( int i = 2; i <= n; i++) {
if (v[i] == i && countDivision[i] == 2) {
for ( int j = 2 * i; j <= n; j += i) {
if (countDivision[j] > 0) {
v[j] = v[j] / i;
countDivision[j]--;
}
}
}
}
vector< int > res;
for ( int i = 2; i <= n; i++) {
if (v[i] == 1 && countDivision[i] == 0)
res.push_back(i);
}
return res;
}
int main()
{
int n = 16;
vector< int > semiPrime = createSemiPrimeSieve(n);
for ( int i = 0; i < semiPrime.size(); i++)
cout << semiPrime[i] << " " ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Vector<Integer> createSemiPrimeSieve( int n)
{
int v[] = new int [n + 1 ];
for ( int i = 1 ; i <= n; i++)
{
v[i] = i;
}
int countDivision[] = new int [n + 1 ];
for ( int i = 0 ; i < n + 1 ; i++)
{
countDivision[i] = 2 ;
}
for ( int i = 2 ; i <= n; i++)
{
if (v[i] == i && countDivision[i] == 2 )
{
for ( int j = 2 * i; j <= n; j += i)
{
if (countDivision[j] > 0 )
{
v[j] = v[j] / i;
countDivision[j]--;
}
}
}
}
Vector<Integer> res = new Vector<>();
for ( int i = 2 ; i <= n; i++)
{
if (v[i] == 1 && countDivision[i] == 0 ) {
res.add(i);
}
}
return res;
}
public static void main(String[] args)
{
int n = 16 ;
Vector<Integer> semiPrime = createSemiPrimeSieve(n);
for ( int i = 0 ; i < semiPrime.size(); i++)
{
System.out.print(semiPrime.get(i) + " " );
}
}
}
|
Python3
def createSemiPrimeSieve(n):
v = [ 0 for i in range (n + 1 )]
for i in range ( 1 , n + 1 ):
v[i] = i
countDivision = [ 0 for i in range (n + 1 )]
for i in range (n + 1 ):
countDivision[i] = 2
for i in range ( 2 , n + 1 , 1 ):
if (v[i] = = i and countDivision[i] = = 2 ):
for j in range ( 2 * i, n + 1 , i):
if (countDivision[j] > 0 ):
v[j] = int (v[j] / i)
countDivision[j] - = 1
res = []
for i in range ( 2 , n + 1 , 1 ):
if (v[i] = = 1 and countDivision[i] = = 0 ):
res.append(i)
return res
if __name__ = = '__main__' :
n = 16
semiPrime = createSemiPrimeSieve(n)
for i in range ( len (semiPrime)):
print (semiPrime[i], end = " " )
|
C#
using System;
using System.Collections;
class GFG
{
static ArrayList createSemiPrimeSieve( int n)
{
int [] v = new int [n + 1];
for ( int i = 1; i <= n; i++)
v[i] = i;
int [] countDivision = new int [n + 1];
for ( int i = 0; i < n + 1; i++)
countDivision[i] = 2;
for ( int i = 2; i <= n; i++)
{
if (v[i] == i && countDivision[i] == 2)
{
for ( int j = 2 * i; j <= n; j += i)
{
if (countDivision[j] > 0)
{
v[j] = v[j] / i;
countDivision[j]--;
}
}
}
}
ArrayList res = new ArrayList();
for ( int i = 2; i <= n; i++)
{
if (v[i] == 1 && countDivision[i] == 0)
res.Add(i);
}
return res;
}
static void Main()
{
int n = 16;
ArrayList semiPrime = createSemiPrimeSieve(n);
for ( int i = 0; i < semiPrime.Count; i++)
Console.Write(( int )semiPrime[i]+ " " );
}
}
|
PHP
<?php
function createSemiPrimeSieve( $n )
{
$v = array_fill (0, $n + 1, 0);
for ( $i = 1; $i <= $n ; $i ++)
$v [ $i ] = $i ;
$countDivision = array_fill (0, $n + 1, 0);
for ( $i = 0; $i < $n + 1; $i ++)
$countDivision [ $i ] = 2;
for ( $i = 2; $i <= $n ; $i ++)
{
if ( $v [ $i ] == $i && $countDivision [ $i ] == 2)
{
for ( $j = 2 * $i ; $j <= $n ; $j += $i )
{
if ( $countDivision [ $j ] > 0)
{
$v [ $j ] = $v [ $j ] / $i ;
$countDivision [ $j ]--;
}
}
}
}
$res = array ();
for ( $i = 2; $i <= $n ; $i ++)
{
if ( $v [ $i ] == 1 && $countDivision [ $i ] == 0)
array_push ( $res , $i );
}
return $res ;
}
$n = 16;
$semiPrime = array ();
$semiPrime = createSemiPrimeSieve( $n );
for ( $i = 0; $i < count ( $semiPrime ); $i ++)
echo $semiPrime [ $i ], " " ;
?>
|
Javascript
<script>
function createSemiPrimeSieve(n)
{
let v = new Array(n + 1).fill(0);
for (let i = 1; i <= n; i++)
v[i] = i;
let countDivision = new Array(n + 1).fill(0);
for (let i = 0; i < n + 1; i++)
countDivision[i] = 2;
for (let i = 2; i <= n; i++)
{
if (v[i] == i && countDivision[i] == 2)
{
for (let j = 2 * i; j <= n; j += i)
{
if (countDivision[j] > 0)
{
v[j] = v[j] / i;
countDivision[j]--;
}
}
}
}
let res = new Array();
for (let i = 2; i <= n; i++)
{
if (v[i] == 1 && countDivision[i] == 0)
res.push(i);
}
return res;
}
let n = 16;
let semiPrime= new Array();
semiPrime = createSemiPrimeSieve(n);
for (let i = 0; i < semiPrime.length; i++)
document.write(semiPrime[i] + " " );
</script>
|
Last Updated :
20 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...