# Perfect totient number

A Perfect totient number is an integer that is equal to the sum of its iterated totients. Perfect totient number is denoted by
For example:

, Now, and 9 == 6 + 2 + 1, Therefore, 9 is a perfect totient number.

### Check if N is a Perfect totient number

Given an integer N, the task is to check N is a Perfect totient number.
Examples:

Input: N = 9
Output: Yes
Input: N = 10
Output: No

Approach: The idea is to find the Euler Totient Value of the given number, suppose we get the Euler Totient Value of N as V, then we will again find the Euler Totient Value of V until the new Euler Totient Value V becomes 1. We will also keep the sum of all the Euler Totient Value values V we got till now and check if the sum is equal to N or not. If equal then the given number is a perfect Euler Totient number.
Below is the implementation of the above approach:

## C++

 // C++ implementation to find // the number of digits in// a Nth fibonacci number #include using namespace std; // Function to find the Totient // number of the given valueint phi(int n) {     // Initialize result as n    int result = n;      // Consider all prime factors     // of n and subtract their     // multiples from result     for (int p = 2; p * p <= n; ++p) {                  // Check if p is a prime factor.         if (n % p == 0) {                          // If yes, then update N             // and result             while (n % p == 0)                 n /= p;             result -= result / p;         }     }      // If n has a prime factor     // greater than sqrt(n)     // (There can be at-most one    // such prime factor)     if (n > 1)         result -= result / n;     return result; }  // Function to check if the number // is a perfect totient numberint isPerfectTotientNum(int n){    // store original value of n    int temp = n;    int sum = 0;         // loop to calculate sum    // of iterated totients    while(n > 1){        sum = sum + phi(n);        n = phi(n);    }    // condition for Perfect    // Totient Number    if(sum == temp)    return true;         return false;} // Driver Code int main(){    int n = 9;     if(isPerfectTotientNum(n))        cout << "Yes";    else        cout << "No";    return 0;}

## Java

 // Java implementation to find the  // number of digits in a Nth // fibonacci numberclass GFG{ // Function to find the Totient // number of the given valuestatic int phi(int n) {          // Initialize result as n    int result = n;      // Consider all prime factors     // of n and subtract their     // multiples from result     for(int p = 2; p * p <= n; ++p)    {                // Check if p is a prime factor       if (n % p == 0)        {                        // If yes, then update N            // and result            while (n % p == 0)           {                n /= p;           }            result -= result / p;        }     }      // If n has a prime factor     // greater than sqrt(n)     // (There can be at-most one    // such prime factor)     if (n > 1)         result -= result / n;     return result; }  // Function to check if the number // is a perfect totient numberstatic boolean isPerfectTotientNum(int n){         // Store original value of n    int temp = n;    int sum = 0;         // Loop to calculate sum    // of iterated totients    while(n > 1)    {        sum = sum + phi(n);        n = phi(n);    }         // Condition for Perfect    // Totient Number    if(sum == temp)       return true;         return false;} // Driver Code public static void main(String[] args){    int n = 9;          if(isPerfectTotientNum(n))    {        System.out.println("Yes");    }    else    {        System.out.println("No");    }}} // This code is contributed by Ritik Bansal

## Python3

 # Python3 implementation to find# the number of digits in# a Nth fibonacci number # Function to find the Totient# number of the given valuedef phi(n):         # Initialize result as n    result = n     # Consider all prime factors    # of n and subtract their    # multiples from result    for p in range(2, n):        if p * p > n:            break         # Check if p is a prime factor.        if (n % p == 0):             # If yes, then update N            # and result            while (n % p == 0):                n //= p                             result -= result // p     # If n has a prime factor    # greater than sqrt(n)    # (There can be at-most one    # such prime factor)    if (n > 1):        result -= result // n             return result # Function to check if the number# is a perfect totient numberdef isPerfectTotientNum(n):         # Store original value of n    temp = n    sum = 0     # Loop to calculate sum    # of iterated totients    while (n > 1):        sum = sum + phi(n)        n = phi(n)             # Condition for Perfect    # Totient Number    if (sum == temp):        return True     return False # Driver Codeif __name__ == '__main__':         n = 9         if (isPerfectTotientNum(n)):        print("Yes")    else:        print("No") # This code is contributed by mohit kumar 29

## C#

 // C# implementation to find the // number of digits in a Nth // fibonacci numberusing System;class GFG{ // Function to find the Totient // number of the given valuestatic int phi(int n) {          // Initialize result as n    int result = n;      // Consider all prime factors     // of n and subtract their     // multiples from result     for(int p = 2; p * p <= n; ++p)    {               // Check if p is a prime factor       if (n % p == 0)        {                        // If yes, then update N            // and result            while (n % p == 0)           {                n /= p;           }            result -= result / p;        }     }      // If n has a prime factor     // greater than sqrt(n)     // (There can be at-most one    // such prime factor)     if (n > 1)         result -= result / n;     return result; }  // Function to check if the number // is a perfect totient numberstatic bool isPerfectTotientNum(int n){         // Store original value of n    int temp = n;    int sum = 0;         // Loop to calculate sum    // of iterated totients    while(n > 1)    {        sum = sum + phi(n);        n = phi(n);    }         // Condition for Perfect    // Totient Number    if(sum == temp)    return true;         return false;} // Driver Code public static void Main(){    int n = 9;          if(isPerfectTotientNum(n))    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by Code_Mech

## Javascript

 

Output:
Yes

Time Complexity: O(n1/2)

Auxiliary Space: O(1)

Previous
Next