Minimum number of Square Free Divisors | Initializing MAX with SQRT ( 10 ^ 6 ) ; 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 ; Print all prime numbers ; This function returns the minimum number of Square Free divisors ; Precomputing Prime Factors ; holds max of max power of all prime factors ; holds the max power of current prime factor ; If number itself is prime , it will be included as answer and thus minimum required answer is 1 ; Driver Code to test above functions
let MAX = 1005 ; function SieveOfEratosthenes ( primes ) { let prime = new Array ( MAX ) ; for ( let i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } for ( let p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * 2 ; i < MAX ; i += p ) { prime [ i ] = false ; } } } for ( let p = 2 ; p < MAX ; p ++ ) { if ( prime [ p ] ) { primes . push ( p ) ; } } } function minimumSquareFreeDivisors ( N ) { let primes = [ ] ; SieveOfEratosthenes ( primes ) ; let max_count = 0 ; for ( let i = 0 ; i < primes . length && primes [ i ] * primes [ i ] <= N ; i ++ ) { if ( N % primes [ i ] == 0 ) { let tmp = 0 ; while ( N % primes [ i ] == 0 ) { tmp ++ ; N = parseInt ( N / primes [ i ] , 10 ) ; } max_count = Math . max ( max_count , tmp ) ; } } if ( max_count == 0 ) { max_count = 1 ; } return max_count ; } let N = 24 ; document . write ( " " + minimumSquareFreeDivisors ( N ) + " " ) ; N = 6 ; document . write ( " " + minimumSquareFreeDivisors ( N ) ) ;