Admirable Numbers - GeeksforGeeks

13 Jul, 2021
Given an integer N, the task is to check if N is an Admirable Number.
An admirable number is a number, if there exists a proper divisor D’ of N such that sigma(N)-2D’ = 2N, where sigma(N) is the sum of all divisors of N  
Examples: 
Input: N = 12 Output: Yes Explanation: 12’s proper divisors are 1, 2, 3, 4, 6, and 12 sigma(N) = 1 + 2 + 3 + 4 + 6 + 12 = 28 sigma(N) – 2D’ = 2N 28 – 2*2 = 2*12 24 == 24 
Input: N = 28 Output: No 
Approach: The idea is to find the sum of all factors of a number that is sigma(N). And then we will find every proper divisor of a number D’ and check if there exists a proper divisor D’ of N such that 
 
Below is the implementation of the above approach:
 
C++
Java
Python3
C#
Javascript
// 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 numberint 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' = 2Nbool 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 numberbool isAdmirableNum(int N){    return check(N);} // Driver codeint main(){    int n = 12;    if (isAdmirableNum(n))        cout << "Yes";    else        cout << "No";    return 0;}
// Java implementation to check if N// is a admirable numberclass GFG{ // Function to calculate the sum of// all divisors of a given numberstatic 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' = 2Nstatic 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 numberstatic boolean isAdmirableNum(int N){    return check(N);} // Driver codepublic 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 implementation to check if# N is an admirable numberimport math # Function to calculate the sum of# all divisors of a given numberdef 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' = 2Ndef 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 numberdef isAdmirableNum(N):         return check(N)     # Driver coden = 12 if (isAdmirableNum(n)):    print("Yes")else:    print("No") # This code is contributed by divyeshrabadiya07
// C# implementation to check if N// is a admirable numberusing System;class GFG{ // Function to calculate the sum of// all divisors of a given numberstatic 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' = 2Nstatic 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 numberstatic bool isAdmirableNum(int N){    return check(N);} // Driver codepublic static void Main(){    int n = 12;         if (isAdmirableNum(n))        Console.Write("Yes");    else        Console.Write("No");}} // This code is contributed by Code_Mech
<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
 
Time Complexity: O(N1/2)
References: OEIS 
shubham prakash 1
Code_Mech
divyeshrabadiya07
todaysgaurav
souravmahato348
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Program to find GCD or HCF of two numbers
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Sieve of Eratosthenes
Program to find sum of elements in a given array
Program for Decimal to Binary Conversion
Program for factorial of a number
Operators in C / C++
The Knight's tour problem | Backtracking-1