K-Primes (Numbers with k prime factors) in a range

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.
        boolean prime[] = new boolean[B+1];
        Arrays.fill(prime,true);
        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.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.
chevron_right

Output:

14 15 18



Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nitin mittal, Ita_c



Article Tags :