Check whether a given Number is Power | Javascript program to find whether a number is power - isolated or not ; for 2 as prime factor ; for odd prime factor ; calculate product of powers and prime factors ; check result for power - isolation ; Driver code
function checkIfPowerIsolated ( num ) { let input = num ; let count = 0 ; let factor = new Array ( 0 ) ; if ( num % 2 == 0 ) { while ( num % 2 == 0 ) { ++ count ; num /= 2 ; } factor [ 2 ] = count ; } for ( let i = 3 ; i * i <= num ; i += 2 ) { count = 0 ; while ( num % i == 0 ) { ++ count ; num /= i ; } if ( count > 0 ) factor [ i ] = count ; } if ( num > 1 ) factor [ num ] = 1 ; let product = 1 ; for ( let i = 0 ; i < num + 1 ; i ++ ) { if ( factor [ i ] > 0 ) product = product * factor [ i ] * i ; } if ( product == input ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } checkIfPowerIsolated ( 12 ) ; checkIfPowerIsolated ( 18 ) ; checkIfPowerIsolated ( 35 ) ;