Count numbers from a given range having exactly 5 distinct factors | javascript program of the above approach ; Stores all prime numbers up to 2 * 10 ^ 5 ; Function to generate all prime numbers up to 2 * 10 ^ 5 using Sieve of Eratosthenes ; Mark 0 and 1 as non - prime ; If i is prime ; Mark all its factors as non - prime ; If current number is prime ; Store the prime ; Function to count numbers in the range [ L , R ] having exactly 5 factors ; Stores the required count ; Driver Code
let N = 200000 ; let prime = new Array ( 20000 ) . fill ( 0 ) ; let index = 0 ; function Sieve ( ) { index = 0 ; let p = new Array ( N + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i <= N ; i ++ ) { p [ i ] = 1 ; } p [ 0 ] = p [ 1 ] = 0 ; for ( let i = 2 ; i * i <= N ; i ++ ) { if ( p [ i ] == 1 ) { for ( let j = i * i ; j <= N ; j += i ) { p [ j ] = 0 ; } } } for ( let i = 1 ; i < N ; i ++ ) { if ( p [ i ] == 1 ) { prime [ index ++ ] = ( Math . pow ( i , 4 ) ) ; } } } function countNumbers ( L , R ) { let Count = 0 ; for ( let i = 0 ; i < index ; i ++ ) { let p = prime [ i ] ; if ( p >= L && p <= R ) { Count ++ ; } } document . write ( Count ) ; } let L = 16 , R = 85000 ; Sieve ( ) ; countNumbers ( L , R ) ;