Maximum number of prime factors a number can have with exactly x factors | Function to count number of prime factors of x ; Count variable is incremented for every prime factor of x ; Loop to count the number of the prime factors of the given number ; Driver Code
function countPrimeFactors ( n ) { if ( n == 1 ) return 0 ; let cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = parseInt ( n / 2 ) ; } for ( let i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { cnt ++ ; n = parseInt ( n / i ) ; } } if ( n > 2 ) cnt ++ ; return cnt ; } let x = 8 ; let prime_factor_cnt = countPrimeFactors ( x ) ; document . write ( prime_factor_cnt ) ;