Count of all values of N in [L, R] such that count of primes upto N is also prime
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 = 10
Output: 4
Explanation:
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 = 12
Output: 5
Explanation:
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(R2)
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++
#include <bits/stdc++.h>
using namespace std;
int count_crazy_primes( int L, int R)
{
int prime[R + 1] = { 0 };
int countPrime[R + 1] = { 0 };
int freqPrime[R + 1] = { 0 };
prime[0] = prime[1] = 1;
for ( int p = 2; p * p <= R; p++) {
if (prime[p] == 0) {
for ( int i = p * p;
i <= R; i += p)
prime[i] = 1;
}
}
for ( int i = 1; i <= R; i++) {
countPrime[i] = countPrime[i - 1];
if (!prime[i]) {
countPrime[i]++;
}
}
for ( int i = 1; i <= R; i++) {
freqPrime[i] = freqPrime[i - 1];
if (!prime[countPrime[i]]) {
freqPrime[i]++;
}
}
return (freqPrime[R]
- freqPrime[L - 1]);
}
int main()
{
int L = 4, R = 12;
cout << count_crazy_primes(L, R);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static int count_crazy_primes( int L, int R)
{
int prime[] = new int [R + 1 ];
int countPrime[] = new int [R + 1 ];
int freqPrime[] = new int [R + 1 ];
prime[ 0 ] = 1 ;
prime[ 1 ] = 1 ;
for ( int p = 2 ; p * p <= R; p++)
{
if (prime[p] == 0 )
{
for ( int i = p * p;
i <= R; i += p)
prime[i] = 1 ;
}
}
for ( int i = 1 ; i <= R; i++)
{
countPrime[i] = countPrime[i - 1 ];
if (prime[i] != 0 )
{
countPrime[i]++;
}
}
for ( int i = 1 ; i <= R; i++)
{
freqPrime[i] = freqPrime[i - 1 ];
if (prime[countPrime[i]] != 0 )
{
freqPrime[i]++;
}
}
return (freqPrime[R] -
freqPrime[L - 1 ]);
}
public static void main (String[] args)
{
int L = 4 , R = 12 ;
System.out.println(count_crazy_primes(L, R));
}
}
|
Python3
def count_crazy_primes(L, R):
prime = [ 0 ] * (R + 1 )
countPrime = [ 0 ] * (R + 1 )
freqPrime = [ 0 ] * (R + 1 )
prime[ 0 ] = prime[ 1 ] = 1
p = 2
while p * p < = R:
if (prime[p] = = 0 ):
for i in range (p * p,
R + 1 , p):
prime[i] = 1
p + = 1
for i in range ( 1 , R + 1 ):
countPrime[i] = countPrime[i - 1 ]
if ( not prime[i]):
countPrime[i] + = 1
for i in range ( 1 , R + 1 ):
freqPrime[i] = freqPrime[i - 1 ]
if ( not prime[countPrime[i]]):
freqPrime[i] + = 1
return (freqPrime[R] -
freqPrime[L - 1 ])
if __name__ = = "__main__" :
L = 4
R = 12
print (count_crazy_primes(L, R))
|
C#
using System;
class GFG{
static int count_crazy_primes( int L,
int R)
{
int []prime = new int [R + 1];
int []countPrime = new int [R + 1];
int []freqPrime = new int [R + 1];
prime[0] = 1;
prime[1] = 1;
for ( int p = 2; p * p <= R; p++)
{
if (prime[p] == 0)
{
for ( int i = p * p; i <= R;
i += p)
prime[i] = 1;
}
}
for ( int i = 1; i <= R; i++)
{
countPrime[i] = countPrime[i - 1];
if (prime[i] != 0)
{
countPrime[i]++;
}
}
for ( int i = 1; i <= R; i++)
{
freqPrime[i] = freqPrime[i - 1];
if (prime[countPrime[i]] != 0)
{
freqPrime[i]++;
}
}
return (freqPrime[R] -
freqPrime[L - 1]);
}
public static void Main(String[] args)
{
int L = 4, R = 12;
Console.WriteLine(
count_crazy_primes(L, R));
}
}
|
Javascript
<script>
function count_crazy_primes(L, R)
{
let prime = Array.from({length: R+1}, (_, i) => 0);
let countPrime = Array.from({length: R+1}, (_, i) => -1);
let freqPrime = Array.from({length: R+1}, (_, i) => 0);
prime[0] = 1;
prime[1] = 1;
for (let p = 2; p * p <= R; p++)
{
if (prime[p] == 0)
{
for (let i = p * p;
i <= R; i += p)
prime[i] = 1;
}
}
for (let i = 1; i <= R; i++)
{
countPrime[i] = countPrime[i - 1];
if (prime[i] != 0)
{
countPrime[i]++;
}
}
for (let i = 1; i <= R; i++) {
freqPrime[i] = freqPrime[i - 1];
if (!prime[countPrime[i]]) {
freqPrime[i]++;
}
}
return (freqPrime[R] -
freqPrime[L - 1]);
}
let L = 4, R = 12;
document.write(count_crazy_primes(L, R));
</script>
|
Time Complexity: O(R*log(log(R)))
Auxiliary Space: O(R)
Last Updated :
16 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...