Queries to find whether a 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 Code
var N = 1000000 ; var fourDiv = Array ( N + 1 ) . fill ( false ) ; function fourDistinctFactors ( ) { var primeAll = Array ( N + 1 ) . fill ( false ) ; for ( var p = 2 ; p * p <= N ; p ++ ) { if ( primeAll [ p ] == false ) { for ( var i = p * 2 ; i <= N ; i += p ) primeAll [ i ] = true ; } } var prime = [ ] ; for ( var p = 2 ; p <= N ; p ++ ) if ( ! primeAll [ p ] ) prime . push ( p ) ; for ( var i = 0 ; i < prime . length ; ++ i ) { var p = prime [ i ] ; if ( p * p * p <= N ) fourDiv [ p * p * p ] = true ; for ( var j = i + 1 ; j < prime . length ; ++ j ) { var q = prime [ j ] ; if ( p * q > N ) break ; fourDiv [ p * q ] = true ; } } } fourDistinctFactors ( ) ; var num = 10 ; if ( fourDiv [ num ] ) document . write ( " " ) ; else document . write ( " " ) ; num = 12 ; if ( fourDiv [ num ] ) document . write ( " " ) ; else document . write ( " " ) ;