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.
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:
- Check if a number has an odd count of odd divisors and even count of even divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Sum of all prime divisors of all the numbers in range L-R
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count all prime numbers in a given range whose sum of digits is also prime
- Maximum count of pairwise co-prime and common divisors of two given numbers
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Divide the two given numbers by their common divisors
- Count of numbers below N whose sum of prime divisors is K
- Sum of product of all integers upto N with their count of divisors
- Find two distinct numbers such that their LCM lies in given range
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of permutations such that sum of K numbers from given range is even
- Count of integers in a range which have even number of odd digits and odd number of even digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.