There are **Q ** queries. Each query is of the form of **L** and **R**. The task is to output sum of number of prime factors of each number in the given range of each query.

Examples:

Input : Q = 2 L = 6, R = 10 L = 1, R = 5 Output : 7 4 For query 1, 6 => 2 [Prime factors are 2 and 3] 7 => 1 8 => 1 9 => 1 10 => 2 Sum = 2 + 1 + 1 + 1 + 2 = 7 For query 2, 1 => 0 2 => 1 3 => 1 4 => 1 5 => 1 Sum = 0 + 1 + 1 + 1 + 1 = 4.

**Method 1 (brute force):**

The idea is to traverse from L to R for each query, and for each number find the number of prime factor and add to the answer.

**Method 2 (efficient approach):**

The idea is to use the Sieve of Eratosthenes method for counting the number of prime factor of composite numbers. Just like, the inner loop of Sieve of Eratosthenes is used to mark composite number. We can use it for incrementing the prime factor of numbers. Instead of marking each array cell as 0 or 1, we can store the number of prime number of that index. And then for each query, find the sum of array from L to R.

Below is the implementation of this approach:

## C++

// C++ program to find sum prime factors // in given range. #include <bits/stdc++.h> #define MAX 1000006 using namespace std; // using sieve method to evaluating // the prime factor of numbers void sieve(int count []) { for (int i = 2; i*i <= MAX; i++) { // if i is prime if (count[i] == 0) { for (int j = 2*i; j < MAX; j+=i) count[j]++; // setting number of prime // factor of a prime number. count[i] = 1; } } } // Returns sum of counts of prime factors in // range from l to r. This function mainly // uses count[] which is filled by Sieve() int query(int count[], int l, int r) { int sum = 0; // finding the sum of number of prime // factor of numbers in a range. for (int i = l; i <= r; i++) sum += count[i]; return sum; } // Driven Program int main() { int count[MAX]; memset(count, 0, sizeof count); sieve(count); cout << query(count, 6, 10) << endl << query(count, 1, 5); return 0; }

## Java

// Java program to find sum prime // factors in given range. class GFG { static final int MAX = 1000006; // using sieve method to evaluating // the prime factor of numbers static void sieve(int count[]) { for (int i = 2; i * i <= MAX; i++) { // if i is prime if (count[i] == 0) { for (int j = 2 * i; j < MAX; j += i) count[j]++; // setting number of prime // factor of a prime number. count[i] = 1; } } } // Returns sum of counts of prime factors in // range from l to r. This function mainly // uses count[] which is filled by Sieve() static int query(int count[], int l, int r) { int sum = 0; // finding the sum of number of prime // factor of numbers in a range. for (int i = l; i <= r; i++) sum += count[i]; return sum; } // Driver code public static void main(String[] args) { int count[] = new int[MAX]; sieve(count); System.out.println(query(count, 6, 10) + " " + query(count, 1, 5)); } } // This code is contributed by Anant Agarwal.

Output:

7 4

This article is contributed by **Anuj chauhan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.