Skip to content
Related Articles

Related Articles

K-Primes (Numbers with k prime factors) in a range
  • Difficulty Level : Hard
  • Last Updated : 21 Apr, 2021

Given three integers A, B and K. We need to find no. of K-prime numbers in the range [A, B]. A number is called K-prime if it has exactly K distinct prime factors. 

Examples: 

Input : A = 4, B = 10, K = 2.
Output : 6 10
Given range is [4, 5, 6, 7, 8, 9, 10].
From the above range 6 and 10 have 2 distinct 
prime factors, 6 = 3*2; 10 = 5*2.

Input : A = 14, B = 18, K = 2.
Output : 14 15 18
Range = [14, 15].
Both 14, 15 and 18 have 2 distinct prime factors,
14 = 7*2, 15 = 3*5 and 18 = 2*3*3

A simple solution is to traverse through given range. For every element of the range, find its prime factors. Finally print all those numbers whose prime factors are k.

An efficient solution is to use Sieve Of Eratosthenes Algorithm  

prime[n] = {true};
for (int p=2; p*p<=n; p++)
{
   // If prime[p] is not changed, then 
   // it is a prime
   if (prime[p] == true)
   {
      // Update all multiples of p
      for (int i=p*2; i<=n; i += p)
         prime[i] = false;
    }
}
 

If we observe the above algorithm clearly it has a property of iterating through all the multiples of prime numbers less than N. So the number of times the algorithm marks a number not prime is equal to the number of prime factors of that number. To achieve this, maintain an array called marked and increase the count of a number every time when it is marked as not prime by the algorithm. And in the next step, we iterate through all the numbers in the range [A, B] and increase our count of k-prime numbers if marked[number] == K.  



C++




// CPP program to count all those numbers in
// given range whose count of prime factors
// is k
#include <bits/stdc++.h>
using namespace std;
 
void printKPFNums(int A, int B, int K)
{
    // Count prime factors of all numbers
    // till B.
    bool prime[B+1] = { true };
    int p_factors[B+1] = { 0 };
    for (int p = 2; p <= B; p++)
        if (p_factors[p] == 0)
            for (int i = p; i <= B; i += p)
                p_factors[i]++;
 
    // Print all numbers with k prime factors
    for (int i = A; i <= B; i++)
        if (p_factors[i] == K)
            cout << i << " ";
}
 
// Driver code
int main()
{
    int A = 14, B = 18, K = 2;
    printKPFNums(A, B, K);
    return 0;
}

Java




// Java program to count
// all those numbers in
// given range whose count
// of prime factors
// is k
 
import java.io.*;
import java.util.*;
 
class GFG {
     
    static void printKPFNums(int A, int B, int K)
    {
        // Count prime factors of all numbers
        // till B.
        int p_factors[] = new int[B+1];
        Arrays.fill(p_factors,0);
 
        for (int p = 2; p <= B; p++)
            if (p_factors[p] == 0)
                for (int i = p; i <= B; i += p)
                    p_factors[i]++;
      
        // Print all numbers with k prime factors
        for (int i = A; i <= B; i++)
            if (p_factors[i] == K)
                System.out.print( i + " ");
    }
      
    // Driver code
    public static void main(String args[])
    {
        int A = 14, B = 18, K = 2;
        printKPFNums(A, B, K);
    }
}
 
 
// This code is contributed
// by Nikita Tiwari.

Python3




# Python 3 program to count
# all those numbers in
# given range whose count
# of prime factors
# is k
 
def printKPFNums(A, B, K) :
 
    # Count prime factors
    # of all numbers
    # till B.
    prime = [ True]*(B+1)
    p_factors= [ 0 ]*(B+1)
    for p in range(2,B+1) :
        if (p_factors[p] == 0)  :
            for i in range(p,B+1,p) :
                p_factors[i] = p_factors[i] + 1
  
    # Print all numbers with
    # k prime factors
    for i in range(A,B+1) :
        if (p_factors[i] == K) :
            print( i ,end=" ")
 
 
# Driver code
A = 14
B = 18
K = 2
printKPFNums(A, B, K)
 
 
# This code is contributed
# by Nikita Tiwari.

C#




// C# program to count all
// those numbers in given
// range whose count of
// prime factors is k
using System;
 
class GFG {
     
    static void printKPFNums(int A, int B,
                                    int K)
    {
        // Count prime factors of
        // all numbers till B.
        bool []prime = new bool[B + 1];
         
        for(int i = 0; i < B + 1; i++)
            prime[i] = true;
             
        int []p_factors = new int[B + 1];
         
        for(int i = 0; i < B + 1; i++)
            p_factors[i] = 0;
 
        for (int p = 2; p <= B; p++)
            if (p_factors[p] == 0)
                for (int i = p; i <= B; i += p)
                    p_factors[i]++;
     
        // Print all numbers with
        // k prime factors
        for (int i = A; i <= B; i++)
            if (p_factors[i] == K)
                Console.Write( i + " ");
    }
     
    // Driver code
    public static void Main()
    {
        int A = 14, B = 18, K = 2;
        printKPFNums(A, B, K);
    }
}
 
// This code is contributed by nitin mittal.

PHP




<?php
// PHP program to count all those numbers
// in given range whose count of prime
// factors is k
 
function printKPFNums($A, $B, $K)
{
    // Count prime factors of all
    // numbers till B.
    $prime = array_fill(true, $B + 1, NULL);
    $p_factors = array_fill(0, $B + 1, NULL);
    for ($p = 2; $p <= $B; $p++)
        if ($p_factors[$p] == 0)
            for ($i = $p; $i <= $B; $i += $p)
                $p_factors[$i]++;
 
    // Print all numbers with
    // k prime factors
    for ($i = $A; $i <= $B; $i++)
        if ($p_factors[$i] == $K)
            echo $i . " ";
}
 
// Driver code
$A = 14;
$B = 18;
$K = 2;
printKPFNums($A, $B, $K);
 
// This code is contributed
// by ChitraNayal
?>

Javascript




<script>
 
// Javascript program to count all those
// numbers in given range whose count
// of prime factors is k
 
// Returns the sum of first
// n odd numbers
function prletKPFNums(A, B, K)
{
     
    // Count prime factors of
    // all numbers till B.
    let prime = [];
       
    for(let i = 0; i < B + 1; i++)
        prime[i] = true;
           
    let p_factors = [];
       
    for(let i = 0; i < B + 1; i++)
        p_factors[i] = 0;
 
    for (let p = 2; p <= B; p++)
        if (p_factors[p] == 0)
            for(let i = p; i <= B; i += p)
                p_factors[i]++;
   
    // Print let all numbers with
    // k prime factors
    for(let i = A; i <= B; i++)
        if (p_factors[i] == K)
            document.write( i + " ");
}
 
// Driver code
let A = 14, B = 18, K = 2;
 
prletKPFNums(A, B, K);
 
// This code is contributed by sanjoy_62
 
</script>

Output: 

14 15 18

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :