Anti-perfect Number
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 numint 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 divisorsint 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 notbool isAntiPerfect(int n){ return divSum(n) == n;}// Driver Codeint 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 approachclass GFG{ // Iterative function to reverse// digits of numstatic 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 divisorsstatic 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 notstatic boolean isAntiPerfect(int n){ return divSum(n) == n;}// Driver Codepublic 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 numdef 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 divisorsdef 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 notdef isAntiPerfect(n): return divSum(n) == n# Driver Code# Given Number NN = 244# Function Callif (isAntiPerfect(N)): print("Yes")else: print("No") # This code is contributed by Vishal Maurya. |
C#
// C# program for the above approachusing System;class GFG{ // Iterative function to reverse// digits of numstatic 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 divisorsstatic 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 notstatic Boolean isAntiPerfect(int n){ return divSum(n) == n;}// Driver Codepublic 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 numfunction 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 divisorsfunction 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 notfunction isAntiPerfect(n){ return divSum(n) == n;}// Driver Code// Given Number Nvar N = 244;// Function Callif (isAntiPerfect(N)) document.write("Yes");else document.write("No"); // This code is contributed by shubhamsingh10</script> |
Yes
Time Complexity: O(sqrt(N))


