Check if a number is a Trojan Number | Function to check if a number can be expressed as x ^ y ; Try all numbers from 2 to sqrt ( n ) as base ; Keep increasing y while power ' p ' is smaller than n . ; Function to check if a number is Strong ; count the number for each prime factor ; minimum number of prime divisors should be 2 ; Function to check if a number is Trojan Number ; Driver Code
function isPerfectPower ( n ) { if ( n == 1 ) { return true ; } for ( var x = 2 ; x <= Math . sqrt ( n ) ; x ++ ) { var y = 2 ; var p = parseInt ( Math . pow ( x , y ) ) ; while ( p <= n && p > 0 ) { if ( p == n ) { return true ; } y ++ ; p = parseInt ( Math . pow ( x , y ) ) ; } } return false ; } function isStrongNumber ( n ) { var count = new Map ( ) ; while ( n % 2 == 0 ) { n = n / 2 ; if ( count . has ( 2 ) ) { count . set ( 2 , count . get ( 2 ) + 1 ) ; } else { count . set ( 2 , 1 ) ; } } for ( var i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { n = n / i ; if ( count . has ( i ) ) { count . set ( i , count . get ( i ) + 1 ) ; } else { count . set ( i , 1 ) ; } } } if ( n > 2 ) { if ( count . has ( n ) ) { count . set ( n , count . get ( n ) + 1 ) ; } else { count . set ( n , 1 ) ; } } var flag = 0 ; const iterator = count [ Symbol . iterator ] ( ) ; let itr = iterator . next ( ) for ( let i = 0 ; i < count . size ; i ++ ) { console . log ( itr . value , itr . done ) if ( itr . value == 1 ) { flag = 1 ; break ; } itr = iterator . next ( ) } if ( flag == 1 ) { return false ; } else { return true ; } } function isTrojan ( n ) { if ( ! isPerfectPower ( n ) && isStrongNumber ( n ) ) { return true ; } else { return false ; } } var n = 108 ; if ( isTrojan ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }