Numbers in range [L, R] such that the count of their divisors is both even and prime
Given a range [L, R], the task is to find the numbers from the range which have the count of their divisors as even as well as prime.
Then, print the count of the numbers found. The values of L and R are less than 10^6 and L< R.
Examples:
Input: L=3, R=9 Output: Count = 3 Explanation: The numbers are 3, 5, 7 Input : L=3, R=17 Output : Count: 6
- The only number that is prime, as well as even, is ‘2’.
- So, we need to find all the numbers within the given range that have exactly 2 divisors,
i.e. prime numbers.
A simple approach:
- Start a loop from ‘l’ to ‘r’ and check whether the number is prime(it will take more time for bigger range).
- If the number is prime then increment the count variable.
- At the end, print the value of count.
An efficient approach:
- We have to count the prime numbers in range [L, R].
- First, create a sieve which will help in determining whether the number is prime or not in O(1) time.
- Then, create a prefix array to store the count of prime numbers where, element at index ‘i’ holds the count of the prime numbers from ‘1’ to ‘i’.
- Now, if we want to find the count of prime numbers in range [L, R], the count will be (sum[R] – sum[L-1])
- Finally, print the result i.e. (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; #define MAX 1000000 // stores whether the number is prime or not bool prime[MAX + 1]; // stores the count of prime numbers // less than or equal to the index int sum[MAX + 1]; // create the sieve void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" and initialize // all the entries as true. A value in prime[i] will // finally be false if 'i' is Not a prime, else true. memset(prime, true, sizeof(prime)); memset(sum, 0, sizeof(sum)); prime[1] = false; for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then it is a prime if (prime[p]) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // stores the prefix sum of number // of primes less than or equal to 'i' for (int i = 1; i <= MAX; i++) { if (prime[i] == true) sum[i] = 1; sum[i] += sum[i - 1]; } } // Driver code int main() { // create the sieve SieveOfEratosthenes(); // 'l' and 'r' are the lower and upper bounds // of the range int l = 3, r = 9; // get the value of count int c = (sum[r] - sum[l - 1]); // display the count cout << "Count: " << c << endl; return 0; } |
chevron_right
filter_none
Java
// Java implementation of the approach class GFG { static final int MAX=1000000; // stores whether the number is prime or not static boolean []prime=new boolean[MAX + 1]; // stores the count of prime numbers // less than or equal to the index static int []sum=new int[MAX + 1]; // create the sieve static void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" and initialize // all the entries as true. A value in prime[i] will // finally be false if 'i' is Not a prime, else true. for(int i=0;i<=MAX;i++) prime[i]=true; for(int i=0;i<=MAX;i++) sum[i]=0; prime[1] = false; for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then it is a prime if (prime[p]) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // stores the prefix sum of number // of primes less than or equal to 'i' for (int i = 1; i <= MAX; i++) { if (prime[i] == true) sum[i] = 1; sum[i] += sum[i - 1]; } } // Driver code public static void main(String []args) { // create the sieve SieveOfEratosthenes(); // 'l' and 'r' are the lower and upper bounds // of the range int l = 3, r = 9; // get the value of count int c = (sum[r] - sum[l - 1]); // display the count System.out.println("Count: " + c); } } |
chevron_right
filter_none
Python 3
# Python 3 implementation of the approach MAX = 1000000 # stores whether the number is prime or not prime = [True] * (MAX + 1) # stores the count of prime numbers # less than or equal to the index sum = [0] * (MAX + 1) # create the sieve def SieveOfEratosthenes(): prime[1] = False p = 2 while p * p <= MAX: # If prime[p] is not changed, # then it is a prime if (prime[p]): # Update all multiples of p i = p * 2 while i <= MAX: prime[i] = False i += p p += 1 # stores the prefix sum of number # of primes less than or equal to 'i' for i in range(1, MAX + 1): if (prime[i] == True): sum[i] = 1 sum[i] += sum[i - 1] # Driver code if __name__ == "__main__": # create the sieve SieveOfEratosthenes() # 'l' and 'r' are the lower and # upper bounds of the range l = 3 r = 9 # get the value of count c = (sum[r] - sum[l - 1]) # display the count print("Count:", c) # This code is contributed by ita_c |
chevron_right
filter_none
C#
// C# implementation of the approach using System; class GFG { static int MAX=1000000; // stores whether the number is prime or not static bool []prime=new bool[MAX + 1]; // stores the count of prime numbers // less than or equal to the index static int []sum=new int[MAX + 1]; // create the sieve static void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" and initialize // all the entries as true. A value in prime[i] will // finally be false if 'i' is Not a prime, else true. for(int i=0;i<=MAX;i++) prime[i]=true; for(int i=0;i<=MAX;i++) sum[i]=0; prime[1] = false; for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then it is a prime if (prime[p]) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // stores the prefix sum of number // of primes less than or equal to 'i' for (int i = 1; i <= MAX; i++) { if (prime[i] == true) sum[i] = 1; sum[i] += sum[i - 1]; } } // Driver code public static void Main() { // create the sieve SieveOfEratosthenes(); // 'l' and 'r' are the lower and upper bounds // of the range int l = 3, r = 9; // get the value of count int c = (sum[r] - sum[l - 1]); // display the count Console.WriteLine("Count: " + c); } } |
chevron_right
filter_none
PHP
<?php // PHP implementation of the approach $MAX = 100000; // Create a boolean array "prime[0..n]" // and initialize all the entries as // true. A value in prime[i] will finally // be false if 'i' is Not a prime, else true. // stores whether the number // is prime or not $prime = array_fill(0, $MAX + 1, true); // stores the count of prime numbers // less than or equal to the index $sum = array_fill(0, $MAX + 1, 0); // create the sieve function SieveOfEratosthenes() { global $MAX, $sum, $prime; $prime[1] = false; for ($p = 2; $p * $p <= $MAX; $p++) { // If prime[p] is not changed, // then it is a prime if ($prime[$p]) { // Update all multiples of p for ($i = $p * 2; $i <= $MAX; $i += $p) $prime[$i] = false; } } // stores the prefix sum of number // of primes less than or equal to 'i' for ($i = 1; $i <= $MAX; $i++) { if ($prime[$i] == true) $sum[$i] = 1; $sum[$i] += $sum[$i - 1]; } } // Driver code // create the sieve SieveOfEratosthenes(); // 'l' and 'r' are the lower // and upper bounds of the range $l = 3; $r = 9; // get the value of count $c = ($sum[$r] - $sum[$l - 1]); // display the count echo "Count: " . $c . "\n"; // This code is contributed by mits ?> |
chevron_right
filter_none
Output:
Count: 3
Recommended Posts:
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Sum of all prime divisors of all the numbers in range L-R
- Count of numbers below N whose sum of prime divisors is K
- Program to find count of numbers having odd number of divisors in given range
- Count numbers from range whose prime factors are only 2 and 3
- Queries for the difference between the count of composite and prime numbers in a given range
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count total divisors of A or B in a given range
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Find numbers with n-divisors in a given range
- Find numbers with K odd divisors in a given range
- Find the number of divisors of all numbers in the range [1, n]
- Count elements in the given range which have maximum number of divisors
- Count the numbers < N which have equal number of divisors as K
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.



