K | CPP program to count all those numbers in given range whose count of prime factors is k ; Returns the sum of first n odd numbers ; Count prime factors of all numbers till B . ; Print all numbers with k prime factors ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printKPFNums ( int A , int B , int K ) { 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 ] ++ ; for ( int i = A ; i <= B ; i ++ ) if ( p_factors [ i ] == K ) cout << i << " ▁ " ; } int main ( ) { int A = 14 , B = 18 , K = 2 ; printKPFNums ( A , B , K ) ; return 0 ; }