Open In App

Anti-perfect Number

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to check if N is an Anti-perfectNumber or not. If N is an Anti-perfectNumber then print “Yes” else print “No”.
 

An anti-perfect Number is a number that is equal to the sum of the reverse of its proper divisors. 
 

Examples: 
 

Input: N = 244 
Output: Yes 
Explanation: 
proper divisors of 24 are 1, 2, 4, 61, 122 
sum of their reverse is 1 + 2 + 4 + 16 + 221 = 244 = N.
Input: N = 28 
Output: No 
 

 

Approach The idea is to find the sum of the reverse of the proper divisors of the number N and check if the sum if equals to N or not. If sum is equals to N, then N is an Anti-perfectNumber then print “Yes” else print “No”.
Below is the implementation of the above approach:
 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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


Javascript




<script>
// Javascript implementation
 
// Iterative function to reverse
// digits of num
function rev(num)
{
    var rev_num = 0;
   
    while (num > 0) {
        rev_num = rev_num * 10 + num % 10;
        num = Math.floor(num / 10);
    }
   
    // Return the reversed num
     
    return rev_num;
}
   
// Function to calculate sum
// of reverse all proper divisors
function divSum(num)
{
    // Final result of summation
    // of divisors
    var result = 0;
   
    // Find all divisors of num
    for (var i = 2; i <= Math.floor(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
     result += 1;
    return result;
}
   
// Function to check if N is
// anti-perfect or not
function isAntiPerfect(n)
{
    return divSum(n) == n;
}
 
// Driver Code
// Given Number N
var N = 244;
 
// Function Call
if (isAntiPerfect(N))
    document.write("Yes");
else
    document.write("No");
   
// This code is contributed by shubhamsingh10
</script>


Output: 

Yes

 

Time Complexity: O(sqrt(N))
 



Last Updated : 25 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads