Deficient Number - GeeksforGeeks

25 Jan, 2022
A number n is said to be Deficient Number if sum of all the divisors of the number denoted by divisorsSum(n) is less than twice the value of the number n. And the difference between these two values is called the deficiency.Mathematically, if below condition holds the number is said to be Deficient:  
divisorsSum(n) < 2 * n
deficiency = (2 * n) - divisorsSum(n)
The first few Deficient Numbers are:1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19 .....Given a number n, our task is to find if this number is Deficient number or not. Examples :  
Input: 21
Output: YES
Divisors are 1, 3, 7 and 21. Sum of divisors is 32.
This sum is less than 2*21 or 42.

Input: 12
Output: NO

Input: 17
Output: YES
 
A Simple solution is to iterate all the numbers from 1 to n and check if the number divides n and calculate the sum. Check if this sum is less than 2 * n or not.Time Complexity of this approach: O ( n )Optimized Solution: If we observe carefully, the divisors of the number n are present in pairs. For example if n = 100, then all the pairs of divisors are: (1, 100), (2, 50), (4, 25), (5, 20), (10, 10)Using this fact we can speed up our program. While checking divisors we will have to be careful if there are two equal divisors as in case of (10, 10). In such case we will take only one of them in calculation of sum.Implementation of Optimized approach  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to implement an Optimized Solution// to check Deficient Number#include <bits/stdc++.h>using namespace std; // Function to calculate sum of divisorsint divisorsSum(int n){    int sum = 0; // Initialize sum of prime factors     // Note that this loop runs till square root of n    for (int i = 1; i <= sqrt(n); i++) {        if (n % i == 0) {            // If divisors are equal, take only one            // of them            if (n / i == i) {                sum = sum + i;            }            else // Otherwise take both            {                sum = sum + i;                sum = sum + (n / i);            }        }    }    return sum;} // Function to check Deficient Numberbool isDeficient(int n){    // Check if sum(n) < 2 * n    return (divisorsSum(n) < (2 * n));} /* Driver program to test above function */int main(){    isDeficient(12) ? cout << "YES\n" : cout << "NO\n";    isDeficient(15) ? cout << "YES\n" : cout << "NO\n";    return 0;}
// Java program to check Deficient Number import java.io.*; class GFG {    // Function to calculate sum of divisors    static int divisorsSum(int n)    {        int sum = 0; // Initialize sum of prime factors         // Note that this loop runs till square root of n        for (int i = 1; i <= (Math.sqrt(n)); i++) {            if (n % i == 0) {                // If divisors are equal, take only one                // of them                if (n / i == i) {                    sum = sum + i;                }                else // Otherwise take both                {                    sum = sum + i;                    sum = sum + (n / i);                }            }        }         return sum;    }     // Function to check Deficient Number    static boolean isDeficient(int n)    {        // Check if sum(n) < 2 * n        return (divisorsSum(n) < (2 * n));    }     /* Driver program to test above function */    public static void main(String args[])    {        if (isDeficient(12))            System.out.println("YES");        else            System.out.println("NO");         if (isDeficient(15))            System.out.println("YES");        else            System.out.println("NO");    }} // This code is contributed by Nikita Tiwari
# Python program to implement an Optimized# Solution to check Deficient Numberimport math # Function to calculate sum of divisorsdef divisorsSum(n) :    sum = 0  # Initialize sum of prime factors     # Note that this loop runs till square    # root of n    i = 1    while i<= math.sqrt(n) :        if (n % i == 0) :             # If divisors are equal, take only one            # of them            if (n // i == i) :                sum = sum + i            else : # Otherwise take both                sum = sum + i;                sum = sum + (n // i)        i = i + 1    return sum   # Function to check Deficient Numberdef isDeficient(n) :     # Check if sum(n) < 2 * n    return (divisorsSum(n) < (2 * n))  # Driver program to test above functionif ( isDeficient(12) ):    print ("YES")else :    print ("NO")if ( isDeficient(15) ) :    print ("YES")else :    print ("NO")  # This Code is contributed by Nikita Tiwari
// C# program to implement an Optimized Solution// to check Deficient Numberusing System; class GFG {     // Function to calculate sum of    // divisors    static int divisorsSum(int n)    {        // Initialize sum of prime factors        int sum = 0;         // Note that this loop runs till        // square root of n        for (int i = 1; i <= (Math.Sqrt(n)); i++) {            if (n % i == 0) {                 // If divisors are equal,                // take only one of them                if (n / i == i) {                    sum = sum + i;                }                else // Otherwise take both                {                    sum = sum + i;                    sum = sum + (n / i);                }            }        }         return sum;    }     // Function to check Deficient Number    static bool isDeficient(int n)    {         // Check if sum(n) < 2 * n        return (divisorsSum(n) < (2 * n));    }     /* Driver program to test above function */    public static void Main()    {        string var = isDeficient(12) ? "YES" : "NO";        Console.WriteLine(var);         string var1 = isDeficient(15) ? "YES" : "NO";        Console.WriteLine(var1);    }} // This code is contributed by vt_m
<?php// PHP program to implement// an Optimized Solution// to check Deficient Number // Function to calculate// sum of divisorsfunction divisorsSum($n){    // Initialize sum of    // prime factors    $sum = 0;     // Note that this loop runs    // till square root of n    for ($i = 1; $i <= sqrt($n); $i++)    {        if ($n % $i==0)        {            // If divisors are equal,            // take only one of them            if ($n / $i == $i)            {                $sum = $sum + $i;            }            // Otherwise take both            else            {                $sum = $sum + $i;                $sum = $sum + ($n / $i);            }        }    }    return $sum;} // Function to check// Deficient Numberfunction isDeficient($n){    // Check if sum(n) < 2 * n    return (divisorsSum($n) < (2 * $n));} // Driver Code$ds = isDeficient(12) ?              "YES\n" :                "NO\n";    echo($ds);$ds = isDeficient(15) ?              "YES\n" :                "NO\n";    echo($ds); // This code is contributed by ajit;.?>
<script> // Javascript program to check Deficient Number     // Function to calculate sum of divisors    function divisorsSum(n)    {        let sum = 0; // Initialize sum of prime factors           // Note that this loop runs till square root of n        for (let i = 1; i <= (Math.sqrt(n)); i++)        {            if (n % i == 0)            {                             // If divisors are equal, take only one                // of them                if (n / i == i) {                    sum = sum + i;                }                else // Otherwise take both                {                    sum = sum + i;                    sum = sum + (n / i);                }            }        }           return sum;    }       // Function to check Deficient Number    function isDeficient(n)    {             // Check if sum(n) < 2 * n        return (divisorsSum(n) < (2 * n));    } // Driver code to test above methods         if (isDeficient(12))            document.write("YES" + "<br/>");        else            document.write("NO" + "<br/>");           if (isDeficient(15))            document.write("YES" + "<br/>");        else           document.write("NO" + "<br/>");   // This code is contributed by avijitmondal1998.</script>
Output :  
NO
YES
Time Complexity : O( sqrt( n )) Auxiliary Space : O( 1 )References : https://en.wikipedia.org/wiki/Deficient_numberThis article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
Akanksha_Rai
avijitmondal1998
amartyaghoshgfg
prime-factor
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Operators in C / C++
The Knight's tour problem | Backtracking-1
Euclidean algorithms (Basic and Extended)
Find minimum number of coins that make a given value
Print all possible combinations of r elements in a given array of size n