Count numbers from a given range having exactly 5 distinct factors | Java Program to implement 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
import java . util . * ; class GFG { static int N = 200000 ; static int prime [ ] = new int [ 20000 ] ; static int index = 0 ; static void Sieve ( ) { index = 0 ; int p [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) { p [ i ] = 1 ; } p [ 0 ] = p [ 1 ] = 0 ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( p [ i ] == 1 ) { for ( int j = i * i ; j <= N ; j += i ) { p [ j ] = 0 ; } } } for ( int i = 1 ; i < N ; i ++ ) { if ( p [ i ] == 1 ) { prime [ index ++ ] = ( int ) ( Math . pow ( i , 4 ) ) ; } } } static void countNumbers ( int L , int R ) { int Count = 0 ; for ( int i = 0 ; i < index ; i ++ ) { int p = prime [ i ] ; if ( p >= L && p <= R ) { Count ++ ; } } System . out . println ( Count ) ; } public static void main ( String [ ] args ) { int L = 16 , R = 85000 ; Sieve ( ) ; countNumbers ( L , R ) ; } }