Perfect totient number | Function to find the Totient number of the given value ; Initialize result as n ; Consider all prime factors of n and subtract their multiples from result ; Check if p is a prime factor ; If yes , then update N and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Function to check if the number is a perfect totient number ; Store original value of n ; Loop to calculate sum of iterated totients ; Condition for Perfect Totient Number ; Driver Code
function phi ( n ) { let result = n ; for ( let p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) { n = parseInt ( n / p ) ; } result -= parseInt ( result / p ) ; } } if ( n > 1 ) result -= parseInt ( result / n ) ; return result ; } function isPerfectTotientNum ( n ) { let temp = n ; let sum = 0 ; while ( n > 1 ) { sum = sum + phi ( n ) ; n = phi ( n ) ; } if ( sum == temp ) return true ; return false ; } let n = 9 ; if ( isPerfectTotientNum ( n ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }