📜  反完美号码

📅  最后修改于: 2021-04-29 05:16:56             🧑  作者: Mango

给定数字N ,任务是检查N是否为Anti-perfectNumber 。如果N是一个反完美编号,则打印“是”,否则打印“否”

例子:

方法的想法是找到数字N的适当除数的逆和,然后检查和是否等于N。如果sum等于N ,则N是一个反完美编号,然后显示“是”,否则显示“否”
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Iterative function to reverse
// digits of num
int rev(int num)
{
    int rev_num = 0;
  
    while (num > 0) {
        rev_num = rev_num * 10
                  + num % 10;
  
        num = num / 10;
    }
  
    // Return the reversed num
    return rev_num;
}
  
// Function to calculate sum
// of reverse all proper divisors
int divSum(int num)
{
    // Final result of summation
    // of divisors
    int result = 0;
  
    // Find all divisors of num
    for (int i = 2; i <= sqrt(num); i++) {
  
        // If 'i' is divisor of 'num'
        if (num % i == 0) {
  
            // If both divisors are same
            // then add it only once
            // else add both
            if (i == (num / i))
                result += rev(i);
            else
                result += (rev(i)
                           + rev(num / i));
        }
    }
  
    // Add 1 to the result as 1
    // is also a divisor
    return (result + 1);
}
  
// Function to check if N is
// anti-perfect or not
bool isAntiPerfect(int n)
{
    return divSum(n) == n;
}
  
// Driver Code
int main()
{
    // Given Number N
    int N = 244;
  
    // Function Call
    if (isAntiPerfect(N))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program for the above approach
class GFG{ 
      
// Iterative function to reverse
// digits of num
static int rev(int num)
{
    int rev_num = 0;
  
    while (num > 0)
    {
        rev_num = rev_num * 10 + 
                      num % 10;
  
        num = num / 10;
    }
  
    // Return the reversed num
    return rev_num;
}
  
// Function to calculate sum
// of reverse all proper divisors
static int divSum(int num)
{
      
    // Final result of summation
    // of divisors
    int result = 0;
  
    // Find all divisors of num
    for(int i = 2; i <= Math.sqrt(num); i++)
    {
         
       // If 'i' is divisor of 'num'
       if (num % i == 0)
       {
             
           // If both divisors are same
           // then add it only once
           // else add both
           if (i == (num / i))
               result += rev(i);
           else
               result += (rev(i) + 
                          rev(num / i));
       }
    }
  
    // Add 1 to the result as 1
    // is also a divisor
    return (result + 1);
}
  
// Function to check if N is
// anti-perfect or not
static boolean isAntiPerfect(int n)
{
    return divSum(n) == n;
}
  
// Driver Code
public static void main (String[] args)
{
      
    // Given Number N
    int N = 244;
  
    // Function Call
    if (isAntiPerfect(N))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
  
// This code is contributed by rock_cool


Python3
# Python3 program for the above approach 
  
# Iterative function to reverse 
# digits of num 
def rev(num):
    rev_num = 0
    while (num > 0) :
        rev_num = rev_num * 10 + num % 10
        num = num // 10 
  
    # Return the reversed num 
    return rev_num
  
# Function to calculate sum 
# of reverse all proper divisors 
def divSum(num) :
    
    # Final result of summation
    # of divisors
    result = 0
  
    # Find all divisors of num
    for i in range(2, int(num**0.5)):
        
        # If 'i' is divisor of 'num'
        if (num % i == 0) :
              
            # If both divisors are same
            # then add it only once
            # else add both
            if (i == (num / i)):
                result += rev(i)
            else:
                result += (rev(i) + rev(num / i))
              
    # Add 1 to the result as 1
    # is also a divisor
    return (result + 1)
  
# Function to check if N is 
# anti-perfect or not
def isAntiPerfect(n):
    return divSum(n) == n
  
# Driver Code
  
# Given Number N
N = 244
  
# Function Call
if (isAntiPerfect(N)):
    print("Yes")
else:
    print("No") 
      
# This code is contributed by Vishal Maurya.


C#
// C# program for the above approach
using System;
class GFG{ 
      
// Iterative function to reverse
// digits of num
static int rev(int num)
{
    int rev_num = 0;
  
    while (num > 0)
    {
        rev_num = rev_num * 10 + 
                      num % 10;
        num = num / 10;
    }
  
    // Return the reversed num
    return rev_num;
}
  
// Function to calculate sum
// of reverse all proper divisors
static int divSum(int num)
{
      
    // Final result of summation
    // of divisors
    int result = 0;
  
    // Find all divisors of num
    for(int i = 2; i <= Math.Sqrt(num); i++)
    {
          
        // If 'i' is divisor of 'num'
        if (num % i == 0)
        {
                  
            // If both divisors are same
            // then add it only once
            // else add both
            if (i == (num / i))
                result += rev(i);
            else
                result += (rev(i) + 
                           rev(num / i));
        }
    }
  
    // Add 1 to the result as 1
    // is also a divisor
    return (result + 1);
}
  
// Function to check if N is
// anti-perfect or not
static Boolean isAntiPerfect(int n)
{
    return divSum(n) == n;
}
  
// Driver Code
public static void Main (String[] args)
{
      
    // Given Number N
    int N = 244;
  
    // Function Call
    if (isAntiPerfect(N))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by shivanisinghss2110


输出:
Yes

时间复杂度: O(sqrt(N))