Perfect Cube | Inserts the prime factor in HashMap if not present if present updates it 's frequency ; A utility function to find all prime factors of a given number N ; Insert the number of 2 s that divide n ; n must be odd at this point So we can skip one element ; while i divides n , insert i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to check if a number is perfect cube ; Iteration in Map ; Driver Code ; Function to check if N is perfect cube or not
function insertPF ( primeFact , fact ) { if ( primeFact . has ( fact ) ) { primeFact . set ( fact , primeFact . get ( fact ) + 1 ) ; } else { primeFact . set ( fact , 1 ) ; } return primeFact ; } function primeFactors ( n ) { var primeFact = new Map ( ) ; while ( n % 2 == 0 ) { primeFact = insertPF ( primeFact , 2 ) ; n = parseInt ( n / 2 ) ; } for ( var i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { primeFact = insertPF ( primeFact , i ) ; n = parseInt ( n / i ) ; } } if ( n > 2 ) primeFact = insertPF ( primeFact , n ) ; return primeFact ; } function perfectCube ( n ) { var primeFact = new Map ( ) ; primeFact = primeFactors ( n ) ; primeFact . forEach ( ( value , key ) => { if ( value % 3 != 0 ) return " " ; } ) ; return " " ; } var N = 216 ; document . write ( perfectCube ( N ) ) ;