Count of numbers below N whose sum of prime divisors is K | Javascript implementation of the approach ; Function to return the count of numbers below N whose sum of prime factors is K ; To store the sum of prime factors for all the numbers ; If i is prime ; Add i to all the numbers which are divisible by i ; To store the count of required numbers ; Return the required count ; Driver code
const MAX = 1000001 ; function countNum ( N , K ) { let sumPF = new Array ( MAX ) . fill ( 0 ) ; for ( let i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == 0 ) { for ( let j = i ; j < N ; j += i ) { sumPF [ j ] += i ; } } } let count = 0 ; for ( let i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == K ) count ++ ; } return count ; } let N = 20 , K = 7 ; document . write ( countNum ( N , K ) ) ;