Queries to find whether a number has exactly four distinct factors or not | Java program to check whether number has exactly four distinct factors or not ; Initialize global variable according to given condition so that it can be accessible to all function ; Function to calculate all number having four distinct distinct factors ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Initialize prime [ ] array which will contains all the primes from 1 - N ; Iterate over all the prime numbers ; Mark cube root of prime numbers ; Mark product of prime numbers ; Driver program
import java . util . * ; class GFG { static int N = ( int ) 1E6 ; static boolean [ ] fourDiv = new boolean [ N + 1 ] ; static void fourDistinctFactors ( ) { boolean [ ] primeAll = new boolean [ N + 1 ] ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( primeAll [ p ] == false ) { for ( int i = p * 2 ; i <= N ; i += p ) primeAll [ i ] = true ; } } ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; for ( int p = 2 ; p <= N ; p ++ ) if ( ! primeAll [ p ] ) prime . add ( p ) ; for ( int i = 0 ; i < prime . size ( ) ; ++ i ) { int p = prime . get ( i ) ; if ( 1L * p * p * p <= N ) fourDiv [ p * p * p ] = true ; for ( int j = i + 1 ; j < prime . size ( ) ; ++ j ) { int q = prime . get ( j ) ; if ( 1L * p * q > N ) break ; fourDiv [ p * q ] = true ; } } } public static void main ( String [ ] args ) { fourDistinctFactors ( ) ; int num = 10 ; if ( fourDiv [ num ] ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; num = 12 ; if ( fourDiv [ num ] ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }