Almost Prime Numbers
Last Updated :
16 Feb, 2023
AA k-Almost Prime Number is a number having exactly k prime factors (not necessarily distinct).
For example,
2, 3, 5, 7, 11 ….(in fact all prime numbers) are 1-Almost Prime Numbers as they have only 1 prime factor (which is themselves). 4, 6, 9…. are 2-Almost Prime Numbers as they have exactly 2 prime factors (4 = 2*2, 6 = 2*3, 9 = 3*3) Similarly, 32 is a 5-Almost Prime Number (32 = 2*2*2*2*2) and so is 72 (2*2*2*3*3) All the 1-Almost Primes are called as Prime Numbers and all the 2-Almost Prime are called semi-primes. The task is to print first n numbers that are k prime.
Examples:
Input: k = 2, n = 5
Output: 4 6 9 10 14
4 has two prime factors, 2 x 2
6 has two prime factors, 2 x 3
Similarly, 9(3 x 3), 10(2 x 5) and 14(2 x 7)
Input: k = 10, n = 2
Output: 1024 1536
1024 and 1536 are first two numbers with 10
prime factors.
We iterate natural numbers and keep printing k-primes till the count of printed k-primes is less than or equal to n. To check if a number is k-prime, we find count of prime factors and if the count is k we consider the number as k-prime.
Below is the implementation of the above approach :
C++
#include<bits/stdc++.h>
using namespace std;
int countPrimeFactors( int n)
{
int count = 0;
while (n%2 == 0)
{
n = n/2;
count++;
}
for ( int i = 3; i <= sqrt (n); i = i+2)
{
while (n%i == 0)
{
n = n/i;
count++;
}
}
if (n > 2)
count++;
return (count);
}
void printKAlmostPrimes( int k, int n)
{
for ( int i=1, num=2; i<=n; num++)
{
if (countPrimeFactors(num) == k)
{
printf ( "%d " , num);
i++;
}
}
return ;
}
int main()
{
int n = 10, k = 2;
printf ( "First %d %d-almost prime numbers : \n" ,
n, k);
printKAlmostPrimes(k, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countPrimeFactors( int n)
{
int count = 0 ;
while (n % 2 == 0 ) {
n = n / 2 ;
count++;
}
for ( int i = 3 ; i <= Math.sqrt(n);
i = i + 2 ) {
while (n % i == 0 ) {
n = n / i;
count++;
}
}
if (n > 2 )
count++;
return (count);
}
static void printKAlmostPrimes( int k, int n)
{
for ( int i = 1 , num = 2 ; i <= n; num++) {
if (countPrimeFactors(num) == k) {
System.out.print(num + " " );
i++;
}
}
return ;
}
public static void main(String[] args)
{
int n = 10 , k = 2 ;
System.out.println( "First " + n + " "
+ k + "-almost prime numbers : " );
printKAlmostPrimes(k, n);
}
}
|
Python3
import math
def countPrimeFactors(n):
count = 0 ;
while (n % 2 = = 0 ):
n = n / 2 ;
count + = 1 ;
i = 3 ;
while (i < = math.sqrt(n)):
while (n % i = = 0 ):
n = n / i;
count + = 1 ;
i = i + 2 ;
if (n > 2 ):
count + = 1 ;
return (count);
def printKAlmostPrimes(k, n):
i = 1 ;
num = 2
while (i < = n):
if (countPrimeFactors(num) = = k):
print (num, end = "");
print ( " " , end = "");
i + = 1 ;
num + = 1 ;
return ;
n = 10 ;
k = 2 ;
print ( "First n k-almost prime numbers:" );
printKAlmostPrimes(k, n);
|
C#
using System;
class GFG
{
static int countPrimeFactors( int n)
{
int count = 0;
while (n % 2 == 0)
{
n = n / 2;
count++;
}
for ( int i = 3; i <= Math.Sqrt(n);
i = i + 2)
{
while (n % i == 0)
{
n = n / i;
count++;
}
}
if (n > 2)
count++;
return (count);
}
static void printKAlmostPrimes( int k, int n)
{
for ( int i = 1, num = 2; i <= n; num++)
{
if (countPrimeFactors(num) == k)
{
Console.Write(num + " " );
i++;
}
}
return ;
}
public static void Main()
{
int n = 10, k = 2;
Console.WriteLine( "First " + n + " "
+ k + "-almost prime numbers : " );
printKAlmostPrimes(k, n);
}
}
|
PHP
<?php
function countPrimeFactors( $n )
{
$count = 0;
while ( $n % 2 == 0)
{
$n = $n / 2;
$count ++;
}
for ( $i = 3; $i <= sqrt( $n ); $i = $i + 2)
{
while ( $n % $i == 0)
{
$n = $n / $i ;
$count ++;
}
}
if ( $n > 2)
$count ++;
return ( $count );
}
function printKAlmostPrimes( $k , $n )
{
for ( $i = 1, $num = 2; $i <= $n ; $num ++)
{
if (countPrimeFactors( $num ) == $k )
{
echo ( $num );
echo ( " " );
$i ++;
}
}
return ;
}
$n = 10;
$k = 2;
echo "First $n $k-almost prime numbers:\n" ;
printKAlmostPrimes( $k , $n );
?>
|
Javascript
<script>
function countPrimeFactors(n)
{
let count = 0;
while (n % 2 == 0) {
n = n / 2;
count++;
}
for (let i = 3; i <= Math.sqrt(n);
i = i + 2) {
while (n % i == 0) {
n = n / i;
count++;
}
}
if (n > 2)
count++;
return (count);
}
function printKAlmostPrimes(k, n)
{
for (let i = 1, num = 2; i <= n; num++) {
if (countPrimeFactors(num) == k) {
document.write(num + " " );
i++;
}
}
return ;
}
let n = 10, k = 2;
document.write( "First " + n + " "
+ k + "-almost prime numbers : " + "<br/>" );
printKAlmostPrimes(k, n);
</script>
|
Output
First 10 2-almost prime numbers :
4 6 9 10 14 15 21 22 25 26
Time Complexity: O(sqrt(n))
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...