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.
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:
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 email@example.com. 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.
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.
- Sum of prime numbers in range [L, R] from given Array for Q queries
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Queries for the difference between the count of composite and prime numbers in a given range
- Queries for counts of multiples in an array
- Prime Factor
- N-th prime factor of a given number
- k-th prime factor of a given number
- Exactly n distinct prime factor numbers from a to b
- Least prime factor of numbers till n
- Count of distinct power of prime factor of N
- Nearest element with at-least one common prime factor
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Find sum of a number and its maximum prime factor
- Sum of largest prime factor of each number less than equal to n
- Find largest prime factor of a number
- Sum of Maximum and Minimum prime factor of every number in the Array
- Count of subarrays whose products don't have any repeating prime factor
- Most frequent factor in a range of integers
- Count all the numbers in a range with smallest factor as K
- Count occurrences of a prime number in the prime factorization of every element from the given range