Given two integers **K** and **N**, the task is to find the count of integers from the range **[2, N – 1]** whose sum of prime divisors is **K**

**Example:**

Input:N = 20, K = 7

Output:2

7 and 10 are the only valid numbers.

sumPFactors(7) = 7

sumPFactors(10) = 2 + 5 = 7

Input:N = 25, K = 5

Output:5

**Approach:** Create an array **sumPF[]** where **sumPF[i]** stores the sum of prime divisors of **i** which can be easily calculated using the approach used in this article. Now, initialise a variable **count = 0** and run a loop from **2** to **N – 1** and for every element **i** if **sumPF[i] = K** then increment the **count**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `#define MAX 1000001 ` ` ` `// Function to return the count of numbers ` `// below N whose sum of prime factors is K ` `int` `countNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `// To store the sum of prime factors ` ` ` `// for all the numbers ` ` ` `int` `sumPF[MAX] = { 0 }; ` ` ` ` ` `for` `(` `int` `i = 2; i < N; i++) { ` ` ` ` ` `// If i is prime ` ` ` `if` `(sumPF[i] == 0) { ` ` ` ` ` `// Add i to all the numbers ` ` ` `// which are divisible by i ` ` ` `for` `(` `int` `j = i; j < N; j += i) { ` ` ` `sumPF[j] += i; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// To store the count of required numbers ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 2; i < N; i++) { ` ` ` `if` `(sumPF[i] == K) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 20, K = 7; ` ` ` ` ` `cout << countNum(N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `MAX = ` `1000001` `; ` ` ` `// Function to return the count of numbers ` `// below N whose sum of prime factors is K ` `static` `int` `countNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `// To store the sum of prime factors ` ` ` `// for all the numbers ` ` ` `int` `[]sumPF = ` `new` `int` `[MAX]; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i < N; i++) ` ` ` `{ ` ` ` ` ` `// If i is prime ` ` ` `if` `(sumPF[i] == ` `0` `) ` ` ` `{ ` ` ` ` ` `// Add i to all the numbers ` ` ` `// which are divisible by i ` ` ` `for` `(` `int` `j = i; j < N; j += i) ` ` ` `{ ` ` ` `sumPF[j] += i; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// To store the count of required numbers ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `2` `; i < N; i++) ` ` ` `{ ` ` ` `if` `(sumPF[i] == K) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `20` `, K = ` `7` `; ` ` ` ` ` `System.out.println(countNum(N, K)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `MAX` `=` `1000001` ` ` `# Function to return the count of numbers ` `# below N whose sum of prime factors is K ` `def` `countNum(N, K) : ` ` ` ` ` `# To store the sum of prime factors ` ` ` `# for all the numbers ` ` ` `sumPF ` `=` `[` `0` `] ` `*` `MAX` `; ` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, N) : ` ` ` ` ` `# If i is prime ` ` ` `if` `(sumPF[i] ` `=` `=` `0` `) : ` ` ` ` ` `# Add i to all the numbers ` ` ` `# which are divisible by i ` ` ` `for` `j ` `in` `range` `(i, N, i) : ` ` ` `sumPF[j] ` `+` `=` `i; ` ` ` ` ` `# To store the count of required numbers ` ` ` `count ` `=` `0` `; ` ` ` `for` `i ` `in` `range` `(` `2` `, N) : ` ` ` `if` `(sumPF[i] ` `=` `=` `K) : ` ` ` `count ` `+` `=` `1` `; ` ` ` ` ` `# Return the required count ` ` ` `return` `count; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `20` `; K ` `=` `7` `; ` ` ` ` ` `print` `(countNum(N, K)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `MAX = 1000001; ` ` ` `// Function to return the count of numbers ` `// below N whose sum of prime factors is K ` `static` `int` `countNum(` `int` `N, ` `int` `K) ` `{ ` ` ` `// To store the sum of prime factors ` ` ` `// for all the numbers ` ` ` `int` `[]sumPF = ` `new` `int` `[MAX]; ` ` ` ` ` `for` `(` `int` `i = 2; i < N; i++) ` ` ` `{ ` ` ` ` ` `// If i is prime ` ` ` `if` `(sumPF[i] == 0) ` ` ` `{ ` ` ` ` ` `// Add i to all the numbers ` ` ` `// which are divisible by i ` ` ` `for` `(` `int` `j = i; j < N; j += i) ` ` ` `{ ` ` ` `sumPF[j] += i; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// To store the count of required numbers ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 2; i < N; i++) ` ` ` `{ ` ` ` `if` `(sumPF[i] == K) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 20, K = 7; ` ` ` ` ` `Console.WriteLine(countNum(N, K)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

2

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:

- Sum of numbers in a range [L, R] whose count of divisors is prime
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Count numbers in a given range whose count of prime factors is a Prime Number
- Count all prime numbers in a given range whose sum of digits is also prime
- Check if count of even divisors of N is equal to count of odd divisors
- Check if a number has an odd count of odd divisors and even count of even divisors
- Minimum numbers needed to express every integer below N as a sum
- Sum of the multiples of two numbers below N
- Count of primes below N which can be expressed as the sum of two primes
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Find sum of divisors of all the divisors of a natural number
- Find the sum of all multiples of 2 and 5 below N
- Sum of all the multiples of 3 and 7 below N
- Find the sum of all Truncatable primes below N
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Maximum count of pairwise co-prime and common divisors of two given numbers
- Count all pairs of divisors of a number N whose sum is coprime with N
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of all prime divisors of all the numbers in range L-R

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.