Given two integers L and R, the task to find the number of Double Prime numbers in the range.
A number N is called double prime when the count of prime numbers in the range 1 to N (excluding 1 and including N) is also prime.
Input: L = 3, R = 10
For 3, we have the range 1, 2, 3, and count of prime number is 2 (which is also a prime no.)
For 4, we have the range 1, 2, 3, 4, and count of a prime number is 2 (which is also a prime no.)
For 5, we have the range 1, 2, 3, 4, 5, and count of a prime number is 3 (which is also a prime no.)
For 6, we have the range 1, 2, 3, 4, 5, 6, and count of prime numbers is 3 (which is also a prime no.)
For 7, we have the range 1, 2, 3, 4, 5, 6, 7, and count of prime numbers is 4 which is nonprime.
Similarly for other numbers till R = 10, the count of prime numbers is nonprime. Hence the count of double prime numbers is 4.
Input: L = 4, R = 12
For the given range there are total 5 double prime numbers.
To solve the problem mentioned above we will use the concept of Sieve to generate prime numbers.
- Generate all prime numbers for 0 to 106 and store in an array.
- Initialize a variable count to keep a track of prime numbers from 1 to some ith position.
- Then for every prime number we will increment the count and also set dp[count] = 1 (where dp is the array which stores a double prime number) indicating the number of numbers from 1 to some ith position that are prime.
- Lastly, find the cumulative sum of dp array so the answer will be dp[R] – dp[L – 1].
Below is the implementation of the above approach:
- Count all prime numbers in a given range whose sum of digits is also prime
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Count numbers from range whose prime factors are only 2 and 3
- Count of natural numbers in range [L, R] which are relatively prime with N
- Count numbers from range whose prime factors are only 2 and 3 using Arrays | Set 2
- 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
- Queries for the difference between the count of composite and prime numbers in a given range
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Prime numbers in a given range using STL | Set 2
- Sum of all the prime numbers in a given range
- Sum of all prime divisors of all the numbers in range L-R
- Print prime numbers in a given range using C++ STL
- Sum of prime numbers in range [L, R] from given Array for Q queries
- K-Primes (Numbers with k prime factors) in a range
- C/C++ Program to find Prime Numbers between given range
- Find and Count total factors of co-prime A or B in a given range 1 to N
- Count of numbers upto M divisible by given Prime Numbers
- Find the highest occurring digit in prime numbers in a range
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.