Dado un número entero N , la tarea es comprobar si N es un Número Admirable.
Un número admirable es un número, si existe un divisor propio D’ de N tal que sigma(N)-2D’ = 2N, donde sigma(N) es la suma de todos los divisores de N
Ejemplos:
Entrada: N = 12
Salida: Sí
Explicación:
Los divisores propios de 12 son 1, 2, 3, 4, 6 y 12
sigma(N) = 1 + 2 + 3 + 4 + 6 + 12 = 28
sigma(N) – 2D ‘ = 2N
28 – 2*2 = 2*12
24 == 24Entrada: N = 28
Salida: No
Enfoque: La idea es encontrar la suma de todos los factores de un número que es sigma(N). Y luego encontraremos todo divisor propio de un número D’ y comprobaremos si existe un divisor propio D’ de N tal que
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check if // N is an admirable number #include <bits/stdc++.h> using namespace std; // Function to calculate the sum of // all divisors of a given number int divSum(int n) { // Sum of divisors int result = 0; // Find all divisors // which divides 'num' for (int i = 2; i <= sqrt(n); i++) { // if 'i' is divisor of 'n' if (n % i == 0) { // if both divisors are same // then add it once else add if (i == (n / i)) result += i; else result += (i + n / i); } } // Add 1 and n to result as above loop // considers proper divisors greater return (result + n + 1); } // Function to check if there // exists a proper divisor // D' of N such that sigma(n)-2D' = 2N bool check(int num) { int sigmaN = divSum(num); // Find all divisors which divides '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)) { if (sigmaN - 2 * i == 2 * num) return true; } else { if (sigmaN - 2 * i == 2 * num) return true; if (sigmaN - 2 * (num / i) == 2 * num) return true; } } } // Check 1 since 1 is also a divisor if (sigmaN - 2 * 1 == 2 * num) return true; return false; } // Function to check if N // is an admirable number bool isAdmirableNum(int N) { return check(N); } // Driver code int main() { int n = 12; if (isAdmirableNum(n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation to check if N // is a admirable number class GFG{ // Function to calculate the sum of // all divisors of a given number static int divSum(int n) { // Sum of divisors int result = 0; // Find all divisors // which divides 'num' for (int i = 2; i <= Math.sqrt(n); i++) { // if 'i' is divisor of 'n' if (n % i == 0) { // if both divisors are same // then add it once else add if (i == (n / i)) result += i; else result += (i + n / i); } } // Add 1 and n to result as above loop // considers proper divisors greater return (result + n + 1); } // Function to check if there // exists a proper divisor // D' of N such that sigma(n)-2D' = 2N static boolean check(int num) { int sigmaN = divSum(num); // Find all divisors which divides '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)) { if (sigmaN - 2 * i == 2 * num) return true; } else { if (sigmaN - 2 * i == 2 * num) return true; if (sigmaN - 2 * (num / i) == 2 * num) return true; } } } // Check 1 since 1 is also a divisor if (sigmaN - 2 * 1 == 2 * num) return true; return false; } // Function to check if N // is an admirable number static boolean isAdmirableNum(int N) { return check(N); } // Driver code public static void main(String[] args) { int n = 12; if (isAdmirableNum(n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by shubham
Python3
# Python3 implementation to check if # N is an admirable number import math # Function to calculate the sum of # all divisors of a given number def divSum(n): # Sum of divisors result = 0 # Find all divisors # which divides 'num' for i in range(2, int(math.sqrt(n)) + 1): # If 'i' is divisor of 'n' if (n % i == 0): # If both divisors are same # then add it once else add if (i == (n // i)): result += i else: result += (i + n // i) # Add 1 and n to result as above loop # considers proper divisors greater return (result + n + 1) # Function to check if there # exists a proper divisor # D' of N such that sigma(n)-2D' = 2N def check(num): sigmaN = divSum(num) # Find all divisors which divides 'num' for i in range(2, int(math.sqrt(num)) + 1): # 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)): if (sigmaN - 2 * i == 2 * num): return True else: if (sigmaN - 2 * i == 2 * num): return True if (sigmaN - 2 * (num // i) == 2 * num): return True # Check 1 since 1 is also a divisor if (sigmaN - 2 * 1 == 2 * num): return True return False # Function to check if N # is an admirable number def isAdmirableNum(N): return check(N) # Driver code n = 12 if (isAdmirableNum(n)): print("Yes") else: print("No") # This code is contributed by divyeshrabadiya07
C#
// C# implementation to check if N // is a admirable number using System; class GFG{ // Function to calculate the sum of // all divisors of a given number static int divSum(int n) { // Sum of divisors int result = 0; // Find all divisors // which divides 'num' for(int i = 2; i <= Math.Sqrt(n); i++) { // If 'i' is divisor of 'n' if (n % i == 0) { // If both divisors are same // then add it once else add if (i == (n / i)) result += i; else result += (i + n / i); } } // Add 1 and n to result as above loop // considers proper divisors greater return (result + n + 1); } // Function to check if there // exists a proper divisor // D' of N such that sigma(n)-2D' = 2N static bool check(int num) { int sigmaN = divSum(num); // Find all divisors which divides '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)) { if (sigmaN - 2 * i == 2 * num) return true; } else { if (sigmaN - 2 * i == 2 * num) return true; if (sigmaN - 2 * (num / i) == 2 * num) return true; } } } // Check 1 since 1 is also a divisor if (sigmaN - 2 * 1 == 2 * num) return true; return false; } // Function to check if N // is an admirable number static bool isAdmirableNum(int N) { return check(N); } // Driver code public static void Main() { int n = 12; if (isAdmirableNum(n)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript implementation to check if N // is a admirable number // Function to calculate the sum of // all divisors of a given number function divSum( n) { // Sum of divisors let result = 0; // Find all divisors // which divides 'num' for ( let i = 2; i <= Math.sqrt(n); i++) { // if 'i' is divisor of 'n' if (n % i == 0) { // if both divisors are same // then add it once else add if (i == (n / i)) result += i; else result += (i + n / i); } } // Add 1 and n to result as above loop // considers proper divisors greater return (result + n + 1); } // Function to check if there // exists a proper divisor // D' of N such that sigma(n)-2D' = 2N function check( num) { let sigmaN = divSum(num); // Find all divisors which divides 'num' for (let 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)) { if (sigmaN - 2 * i == 2 * num) return true; } else { if (sigmaN - 2 * i == 2 * num) return true; if (sigmaN - 2 * (num / i) == 2 * num) return true; } } } // Check 1 since 1 is also a divisor if (sigmaN - 2 * 1 == 2 * num) return true; return false; } // Function to check if N // is an admirable number function isAdmirableNum( N) { return check(N); } // Driver code let n = 12; if (isAdmirableNum(n)) document.write("Yes"); else document.write("No"); // This code is contributed by todaysgaurav </script>
Yes
Complejidad de tiempo: O(N 1/2 )
Referencias: OEIS