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 = 14B = 18K = 2printKPFNums(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 ?> |
Output:
14 15 18
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Number which has the maximum number of distinct prime factors in the range M to N
- Count all prime numbers in a given range whose sum of digits is also prime
- Find number of factors of N when location of its two factors whose product is N is given
- Number of distinct prime factors of first n natural numbers
- Common prime factors of two numbers
- Count common prime factors of two numbers
- Find sum of exponents of prime factors of numbers 1 to N
- Sum of all odd factors of numbers in the range [l, r]
- Sum of all even factors of numbers in the range [l, r]
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
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.

