Count of all values of N in [ L , R ] such that count of primes upto N is also prime | Function to count the number of crazy primes in the given range [ L , R ] ; Stores all primes ; Stores count of primes ; Stores if frequency of primes is a prime or not upto each index ; Sieve of Eratosthenes ; Count primes ; If i is a prime ; Stores frequency of primes ; If the frequency of primes is a prime ; Increase count of required numbers ; Return the required count ; Given range ; Function call
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 ) ) ;