Given Q queries where each query consists of an integer range [L, R], the task is to find the sum of the integers from the given range whose count of divisors is prime.
Examples:
Input: Q[][] = {{2, 4}}
Output:
9
All the numbers in the range have only 2 divisors
which is prime.
(2 + 3 + 4) = 9.Input: Q[][] = {{15, 17}, {2, 12}}
Output:
33
41
Approach: Find all the prime numbers and the count of divisors for each element up to a limit N using Sieve of Eratosthenes. Now create a prefix sum array sum[] where sum[i] will store the sum of elements from the range [0, i] whose count of divisors is prime using the Sieve array created earlier. Now every query can be answered in O(1) as sum[r] – sum[l – 1].
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int N = 100000; // prime[i] stores 1 if i is prime int prime[N]; // divi[i] stores the count of // divisors of i int divi[N]; // sum[i] will store the sum of all // the integers from 0 to i whose // count of divisors is prime int sum[N]; // Function for Sieve of Eratosthenes void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be 0 if i is Not a prime, else true. for (int i = 0; i < N; i++) prime[i] = 1; // 0 and 1 is not prime prime[0] = prime[1] = 0; for (int p = 2; p * p < N; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == 1) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i < N; i += p) prime[i] = 0; } } } // Function to count the divisors void DivisorCount() { // For each number i we will go to each of // the multiple of i and update the count // of divisor of its multiple j as i is one // of the factor of j for (int i = 1; i < N; i++) { for (int j = i; j < N; j += i) { divi[j]++; } } } // Function for pre-computation void pre() { for (int i = 1; i < N; i++) { // If count of divisors of i is prime if (prime[divi[i]] == 1) { sum[i] = i; } } // taking prefix sum for (int i = 1; i < N; i++) sum[i] += sum[i - 1]; } // Driver code int main() { int l = 5, r = 8; // Find all the prime numbers till N SieveOfEratosthenes(); // Update the count of divisors // of all the numbers till N DivisorCount(); // Precomputation for the prefix sum array pre(); // Perform query cout << sum[r] - sum[l - 1]; return 0; } |
Java
//Java implementation of above approach import java.util.*; class GFG { static int N = 100000; // prime[i] stores 1 if i is prime static int prime[] = new int[N]; // divi[i] stores the count of // divisors of i static int divi[] = new int[N]; // sum[i] will store the sum of all // the integers from 0 to i whose // count of divisors is prime static int sum[] = new int[N]; // Function for Sieve of Eratosthenes static void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. // A value in prime[i] will finally be 0 // if i is Not a prime, else true. for (int i = 0; i < N; i++) prime[i] = 1; // 0 and 1 is not prime prime[0] = prime[1] = 0; for (int p = 2; p * p < N; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == 1) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i < N; i += p) prime[i] = 0; } } } // Function to count the divisors static void DivisorCount() { // For each number i we will go to each of // the multiple of i and update the count // of divisor of its multiple j as i is one // of the factor of j for (int i = 1; i < N; i++) { for (int j = i; j < N; j += i) { divi[j]++; } } } // Function for pre-computation static void pre() { for (int i = 1; i < N; i++) { // If count of divisors of i is prime if (prime[divi[i]] == 1) { sum[i] = i; } } // taking prefix sum for (int i = 1; i < N; i++) sum[i] += sum[i - 1]; } // Driver code public static void main(String args[]) { int l = 5, r = 8; // Find all the prime numbers till N SieveOfEratosthenes(); // Update the count of divisors // of all the numbers till N DivisorCount(); // Precomputation for the prefix sum array pre(); // Perform query System.out.println( sum[r] - sum[l - 1]); } } // This code is contributed by Arnab Kundu |
Python3
# Python3 implementation of the approach from math import sqrt N = 100000; # Create a boolean array "prime[0..n]" and # initialize all entries it as true. # A value in prime[i] will finally be 0 if # i is Not a prime, else true. # prime[i] stores 1 if i is prime prime = [1] * N; # divi[i] stores the count of # divisors of i divi = [0] * N; # sum[i] will store the sum of all # the integers from 0 to i whose # count of divisors is prime sum = [0] * N; # Function for Sieve of Eratosthenes def SieveOfEratosthenes() : for i in range(N) : prime[i] = 1; # 0 and 1 is not prime prime[0] = prime[1] = 0; for p in range(2, int(sqrt(N)) + 1) : # If prime[p] is not changed, # then it is a prime if (prime[p] == 1) : # Update all multiples of p greater than or # equal to the square of it # numbers which are multiple of p and are # less than p^2 are already been marked. for i in range(p * p, N, p) : prime[i] = 0; # Function to count the divisors def DivisorCount() : # For each number i we will go to each of # the multiple of i and update the count # of divisor of its multiple j as i is one # of the factor of j for i in range(1, N) : for j in range(i, N , i) : divi[j] += 1; # Function for pre-computation def pre() : for i in range(1, N) : # If count of divisors of i is prime if (prime[divi[i]] == 1) : sum[i] = i; # taking prefix sum for i in range(1, N) : sum[i] += sum[i - 1]; # Driver code if __name__ == "__main__" : l = 5; r = 8; # Find all the prime numbers till N SieveOfEratosthenes(); # Update the count of divisors # of all the numbers till N DivisorCount(); # Precomputation for the prefix sum array pre(); # Perform query print(sum[r] - sum[l - 1]); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { static int N = 100000; // prime[i] stores 1 if i is prime static int []prime = new int[N]; // divi[i] stores the count of // divisors of i static int []divi = new int[N]; // sum[i] will store the sum of all // the integers from 0 to i whose // count of divisors is prime static int []sum = new int[N]; // Function for Sieve of Eratosthenes static void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" // and initialize all entries it as true. // A value in prime[i] will finally be 0 // if i is Not a prime, else true. for (int i = 0; i < N; i++) prime[i] = 1; // 0 and 1 is not prime prime[0] = prime[1] = 0; for (int p = 2; p * p < N; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == 1) { // Update all multiples of p greater than // or equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i < N; i += p) prime[i] = 0; } } } // Function to count the divisors static void DivisorCount() { // For each number i we will go to each of // the multiple of i and update the count // of divisor of its multiple j as i is one // of the factor of j for (int i = 1; i < N; i++) { for (int j = i; j < N; j += i) { divi[j]++; } } } // Function for pre-computation static void pre() { for (int i = 1; i < N; i++) { // If count of divisors of i is prime if (prime[divi[i]] == 1) { sum[i] = i; } } // taking prefix sum for (int i = 1; i < N; i++) sum[i] += sum[i - 1]; } // Driver code public static void Main(String []args) { int l = 5, r = 8; // Find all the prime numbers till N SieveOfEratosthenes(); // Update the count of divisors // of all the numbers till N DivisorCount(); // Precomputation for the prefix sum array pre(); // Perform query Console.WriteLine( sum[r] - sum[l - 1]); } } // This code is contributed by 29AjayKumar |
12
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:
- Count all prime numbers in a given range whose sum of digits is also prime
- Count of numbers below N whose sum of prime divisors is K
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Sum of all prime divisors of all the numbers in range L-R
- 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
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Count numbers from range whose prime factors are only 2 and 3
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- 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
- Maximum count of pairwise co-prime and common divisors of two given numbers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Count all pairs of divisors of a number N whose sum is coprime with N
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Program to find count of numbers having odd number of divisors in given range
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Divisors of n-square that are not divisors of n
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.

