📜  号码不足

📅  最后修改于: 2021-05-04 23:40:04             🧑  作者: Mango

如果由divisorsSum(n)表示的数字的所有除数的总和小于数字n的值的两倍,则将数字n称为不足数。这两个值之间的差异称为不足
从数学上讲,如果满足以下条件,则该数字称为“不足”:

divisorsSum(n) < 2 * n
deficiency = (2 * n) - divisorsSum(n)

前几个不足编号是:
1、2、3、4、5、7、8、9、10、11、13、14、15、16、17、19…..
给定数字n,我们的任务是查找该数字是否为不足数字。
例子 :

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

一个简单的解决方案是将所有数字从1迭代到n,然后检查数字是否除以n并计算总和。检查此总和是否小于2 * n。
这种方法的时间复杂度:O(n)
优化的解决方案:
如果我们仔细观察,数字n的除数是成对出现的。例如,如果n = 100,则所有对数除数分别为:(1,100),(2,50),(4,25),(5,20),(10,10)
利用这一事实,我们可以加快程序速度。
在检查除数时,我们必须注意是否存在两个相等的除数,例如(10,10)。在这种情况下,我们在计算总和时将只采用其中之一。
实施优化方法

C++
// C++ program to implement an Optimized Solution
// to check Deficient Number
#include 
using namespace std;
 
// Function to calculate sum of divisors
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 <= 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
bool 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
// 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
# Python program to implement an Optimized
# Solution to check Deficient Number
import math
 
# Function to calculate sum of divisors
def 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 Number
def isDeficient(n) :
 
    # Check if sum(n) < 2 * n
    return (divisorsSum(n) < (2 * n))
  
# Driver program to test above function
if ( isDeficient(12) ):
    print "YES"
else :
    print "NO"
if ( isDeficient(15) ) :
    print "YES"
else :
    print "NO"
  
# This Code is contributed by Nikita Tiwari


C#
// C# program to implement an Optimized Solution
// to check Deficient Number
using 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


Javascript


输出 :

NO
YES