# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 value  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 number  int 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 number  class GFG{     // Function to find the Totient   // number of the given value  static 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 number  static 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 value  def 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 number  def 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 Code  if __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 number  using System;  class GFG{     // Function to find the Totient   // number of the given value  static 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 number  static 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

Output:

Yes


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.