Skip to content
Related Articles

Related Articles

Perfect totient number

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Jul, 2021

A Perfect totient number is an integer that is equal to the sum of its iterated totients. Perfect totient number is denoted by \varphi^i(n)
For example: 
 

\varphi^i (9) = 6    , Now, \varphi^i (6) = 2    \varphi^i (2) = 1    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 <bits/stdc++.h>
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

Javascript




<script>
// Javascript implementation to find the 
// number of digits in a Nth
// fibonacci number
 
    // Function to find the Totient
    // number of the given value
    function phi( n)
    {
 
        // Initialize result as n
        let result = n;
 
        // Consider all prime factors
        // of n and subtract their
        // multiples from result
        for ( let 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 = parseInt(n/p);
                }
                result -= parseInt(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 -= parseInt(result / n);
        return result;
    }
 
    // Function to check if the number
    // is a perfect totient number
    function isPerfectTotientNum( n) {
 
        // Store original value of n
        let temp = n;
        let 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
    let n = 9;
 
    if (isPerfectTotientNum(n)) {
        document.write("Yes");
    } else {
        document.write("No");
    }
// This code is contributed by aashish1995
</script>

Output: 

Yes

 

Time Complexity: O(n1/2)

Auxiliary Space: O(1)

References: https://en.wikipedia.org/wiki/Perfect_totient_number
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!