📜  完美的收件人数

📅  最后修改于: 2021-05-06 22:11:54             🧑  作者: Mango

完善的目标人人数是一个等于其重复目标人的总和的整数。完美的收件人号码用表示\varphi^i(n)
例如:

检查N是否是完美的目标数

给定一个整数N ,任务是检查N是一个完美的目标数字。
例子:

方法:想法是找到给定数字的欧拉Totient值,假设我们得到N的欧拉Totient值为V,然后我们将再次找到V的欧拉Totient值,直到新的欧拉Totient值V变为1。还将保留到现在为止的所有欧拉Totient值V的总和,并检查总和是否等于N。如果相等,则给定的数字是一个完美的欧拉Totient数。
下面是上述方法的实现:

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


Javascript


输出:
Yes

参考: https : //en.wikipedia.org/wiki/Perfect_totient_number