Given two positive integers **L** and **R**, the task is to find out the total number of values between range **[L, R]** such that the count of prime numbers from **1** to **N** is also prime.**Examples:**

Input:L = 3, R = 10Output:4Explanation:

Number of primes upto 3, 4, 5, 6, 7, 8, 9 and 10 are 2, 2, 3, 3, 4, 4, 4 and 4 respectively. So, there are a total 4 such numbers {3, 4, 5, 6}[3, 10].Input:L = 4, R = 12Output:5Explanation:

Number of primes upto 4, 5, 6, 7, 8, 9, 10, 11 and 12 are 2, 3, 3, 4, 4, 4, 4, 5 and 5 respectively. So, there are total 5 such numbers {4, 5, 6, 11, 12} which satisfy the condition in the range [4, 12].

**Naive Approach:**

The simplest approach to solve the problem is to traverse for all values in the range [1, L – 1] **count** the number of primes in that range. Once, calculated, check if the **count** is prime or not. Now, start traversing the values in the range **[L, R]** one by one and check if the number is prime or not and increase the count accordingly. For every updated count check if it is prime or not and accordingly update the count of required numbers from the given range. **Time Complexity:** O(R^{2})**Efficient Approach:**

The above approach can be further optimized by the Sieve of Eratosthenes. Follow the steps below to solve the problem:

- Find all prime numbers up to
**R**using a sieve. - Maintain a frequency array to store the count of primes up to for all values up to
**R**. - Create another count array(say
**freqPrime[]**) and add 1 at position**i**if the cumulative count of total primes up to**i**is itself a prime number. - Now for any range
**L to R**, then the number of crazy primes can be calculated by**freqPrime[R] – freqPrime[L – 1]**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the number of` `// crazy primes in the given range [L, R]` `int` `count_crazy_primes(` `int` `L, ` `int` `R)` `{` ` ` `// Stores all primes` ` ` `int` `prime[R + 1] = { 0 };` ` ` `// Stores count of primes` ` ` `int` `countPrime[R + 1] = { 0 };` ` ` `// Stores if frequency of` ` ` `// primes is a prime or not` ` ` `// upto each index` ` ` `int` `freqPrime[R + 1] = { 0 };` ` ` `prime[0] = prime[1] = 1;` ` ` `// Sieve of Eratosthenes` ` ` `for` `(` `int` `p = 2; p * p <= R; p++) {` ` ` `if` `(prime[p] == 0) {` ` ` `for` `(` `int` `i = p * p;` ` ` `i <= R; i += p)` ` ` `prime[i] = 1;` ` ` `}` ` ` `}` ` ` `// Count primes` ` ` `for` `(` `int` `i = 1; i <= R; i++) {` ` ` `countPrime[i] = countPrime[i - 1];` ` ` `// If i is a prime` ` ` `if` `(!prime[i]) {` ` ` `countPrime[i]++;` ` ` `}` ` ` `}` ` ` `// Stores frequency of primes` ` ` `for` `(` `int` `i = 1; i <= R; i++) {` ` ` `freqPrime[i] = freqPrime[i - 1];` ` ` `// If the frequency of primes` ` ` `// is a prime` ` ` `if` `(!prime[countPrime[i]]) {` ` ` `// Increase count of` ` ` `// required numbers` ` ` `freqPrime[i]++;` ` ` `}` ` ` `}` ` ` `// Return the required count` ` ` `return` `(freqPrime[R]` ` ` `- freqPrime[L - 1]);` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Range` ` ` `int` `L = 4, R = 12;` ` ` `// Function Call` ` ` `cout << count_crazy_primes(L, R);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG{ ` `// Function to count the number of` `// crazy primes in the given range [L, R]` `static` `int` `count_crazy_primes(` `int` `L, ` `int` `R)` `{` ` ` ` ` `// Stores all primes` ` ` `int` `prime[] = ` `new` `int` `[R + ` `1` `];` ` ` `// Stores count of primes` ` ` `int` `countPrime[] = ` `new` `int` `[R + ` `1` `];` ` ` `// Stores if frequency of` ` ` `// primes is a prime or not` ` ` `// upto each index` ` ` `int` `freqPrime[] = ` `new` `int` `[R + ` `1` `];` ` ` ` ` `prime[` `0` `] = ` `1` `;` ` ` `prime[` `1` `] = ` `1` `;` ` ` ` ` `// Sieve of Eratosthenes` ` ` `for` `(` `int` `p = ` `2` `; p * p <= R; p++)` ` ` `{` ` ` `if` `(prime[p] == ` `0` `)` ` ` `{` ` ` `for` `(` `int` `i = p * p;` ` ` `i <= R; i += p)` ` ` `prime[i] = ` `1` `;` ` ` `}` ` ` `}` ` ` `// Count primes` ` ` `for` `(` `int` `i = ` `1` `; i <= R; i++)` ` ` `{` ` ` `countPrime[i] = countPrime[i - ` `1` `];` ` ` ` ` `// If i is a prime` ` ` `if` `(prime[i] != ` `0` `)` ` ` `{` ` ` `countPrime[i]++;` ` ` `}` ` ` `}` ` ` `// Stores frequency of primes` ` ` `for` `(` `int` `i = ` `1` `; i <= R; i++)` ` ` `{` ` ` `freqPrime[i] = freqPrime[i - ` `1` `];` ` ` ` ` `// If the frequency of primes` ` ` `// is a prime` ` ` `if` `(prime[countPrime[i]] != ` `0` `)` ` ` `{` ` ` ` ` `// Increase count of` ` ` `// required numbers` ` ` `freqPrime[i]++;` ` ` `}` ` ` `}` ` ` `// Return the required count` ` ` `return` `(freqPrime[R] - ` ` ` `freqPrime[L - ` `1` `]);` `}` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `// Given range` ` ` `int` `L = ` `4` `, R = ` `12` `;` ` ` `// Function call` ` ` `System.out.println(count_crazy_primes(L, R));` `} ` `} ` `// This code is contributed by Pratima Pandey ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System;` `class` `GFG{ ` ` ` `// Function to count the number of` `// crazy primes in the given range [L, R]` `static` `int` `count_crazy_primes(` `int` `L, ` ` ` `int` `R)` `{ ` ` ` `// Stores all primes` ` ` `int` `[]prime = ` `new` `int` `[R + 1];` ` ` ` ` `// Stores count of primes` ` ` `int` `[]countPrime = ` `new` `int` `[R + 1];` ` ` ` ` `// Stores if frequency of` ` ` `// primes is a prime or not` ` ` `// upto each index` ` ` `int` `[]freqPrime = ` `new` `int` `[R + 1];` ` ` ` ` `prime[0] = 1;` ` ` `prime[1] = 1;` ` ` ` ` `// Sieve of Eratosthenes` ` ` `for` `(` `int` `p = 2; p * p <= R; p++)` ` ` `{` ` ` `if` `(prime[p] == 0)` ` ` `{` ` ` `for` `(` `int` `i = p * p; i <= R; ` ` ` `i += p)` ` ` `prime[i] = 1;` ` ` `}` ` ` `}` ` ` ` ` `// Count primes` ` ` `for` `(` `int` `i = 1; i <= R; i++)` ` ` `{` ` ` `countPrime[i] = countPrime[i - 1];` ` ` ` ` `// If i is a prime` ` ` `if` `(prime[i] != 0)` ` ` `{` ` ` `countPrime[i]++;` ` ` `}` ` ` `}` ` ` ` ` `// Stores frequency of primes` ` ` `for` `(` `int` `i = 1; i <= R; i++)` ` ` `{` ` ` `freqPrime[i] = freqPrime[i - 1];` ` ` ` ` `// If the frequency of primes` ` ` `// is a prime` ` ` `if` `(prime[countPrime[i]] != 0)` ` ` `{ ` ` ` `// Increase count of` ` ` `// required numbers` ` ` `freqPrime[i]++;` ` ` `}` ` ` `}` ` ` ` ` `// Return the required count` ` ` `return` `(freqPrime[R] - ` ` ` `freqPrime[L - 1]);` `}` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `// Given range` ` ` `int` `L = 4, R = 12;` ` ` ` ` `// Function call` ` ` `Console.WriteLine(` ` ` `count_crazy_primes(L, R));` `} ` `} ` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

**Output:**

5

**Time Complexity:** O(R*log(log(R))) **Auxillary Space:** O(R)

## Recommended Posts:

- Count all prime numbers in a given range whose sum of digits is also prime
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
- Count of interesting primes upto N
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Length of largest sub-array having primes strictly greater than non-primes
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- All possible values of floor(N/K) for all values of K
- Count of numbers upto M divisible by given Prime Numbers
- Find smallest perfect square number A such that N + A is also a perfect square number
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Find the prime numbers which can written as sum of most consecutive primes
- Prime points (Points that split a number into two primes)
- K-Primes (Numbers with k prime factors) in a range
- Check if the sum of primes is divisible by any prime from the array
- Largest subsequence such that all indices and all values are multiples individually
- Sum of largest divisor of numbers upto N not divisible by given prime number P

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.