Dado un número N , la tarea es verificar si N es un número anti-perfecto o no. Si N es un número anti-perfecto, imprima «Sí», de lo contrario, imprima «No» .
Un número antiperfecto es un número que es igual a la suma del reverso de sus divisores propios.
Ejemplos:
Entrada: N = 244
Salida: Sí
Explicación:
los divisores propios de 24 son 1, 2, 4, 61, 122
la suma de su reverso es 1 + 2 + 4 + 16 + 221 = 244 = N.
Entrada: N = 28
Salida: No
Enfoque La idea es encontrar la suma del reverso de los divisores propios del número N y verificar si la suma es igual a N o no. Si la suma es igual a N , entonces N es un número anti-perfecto, luego imprime «Sí» , de lo contrario, imprime «No» .
A continuación se muestra la implementación del enfoque anterior:
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>
Yes
Complejidad del tiempo: O(sqrt(N))