📌  相关文章
📜  数量充裕

📅  最后修改于: 2021-04-26 18:59:09             🧑  作者: Mango

如果由sum(n)表示的数字的所有适当除数的总和大于数字n的值,则将数字n称为丰富数。这两个值之间的差异称为丰度
从数学上讲,如果满足以下条件,则将该数字称为“充裕的数字”:

sum(n)> n
abundance =  sum(n) - n
sum(n): aliquot sum - The sum of all proper divisors of n

给定数字n,我们的任务是查找该数字是否为充裕数字。
前几个丰富数字是:12、18、20、24、30、36、40、42、48、54、56、60、66…..

大量数字

例子 :

Input: 21
Output: NO

Input: 12
Output: YES

Input: 17
Output: NO

方法1

一个简单的解决方案是迭代从1到n-1的所有数字,并检查数字是否除以n并计算总和。检查此总和是否大于n。
这种方法的时间复杂度:O(n)

优化的解决方案:

如果我们仔细观察,数字n的除数是成对出现的。例如,如果n = 100,则所有对数除数为:(1,100),(2,50),(4,25),(5,20),(10,10)
利用这一事实,我们可以加快程序速度。在检查除数时,我们必须注意是否存在两个相等的除数,例如(10,10)。在这种情况下,我们在计算总和时将只采用其中之一。
从所有除数的总和中减去数字n,即可得到适当除数的总和。

C++
// An Optimized Solution to check Abundant Number
#include 
using namespace std;
 
// Function to calculate sum of divisors
int getSum(int n)
{
    int sum = 0;
 
    // 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);
            }
        }
    }
 
    // calculate sum of all proper divisors only
    sum = sum - n;
    return sum;
}
 
// Function to check Abundant Number
bool checkAbundant(int n)
{
    // Return true if sum of divisors is greater
    // than n.
    return (getSum(n) > n);
}
 
/* Driver program to test above function */
int main()
{
    checkAbundant(12)? cout << "YES\n" : cout << "NO\n";
    checkAbundant(15)? cout << "YES\n" : cout << "NO\n";
    return 0;
}


Java
// An Optimized Solution to check Abundant Number
// in JAVA
import java.io.*;
import java.math.*;
 
// Function to calculate sum of divisors
class GFG{
    static int getSum(int n)
    {
        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);
                }
            }
        }
   
        // calculate sum of all proper divisors
       // only
        sum = sum - n;
        return sum;
    }
   
    // Function to check Abundant Number
    static boolean checkAbundant(int n)
    {
      // Return true if sum of divisors is
      // greater than n.
      return (getSum(n) > n);
    }
   
    /* Driver program to test above function */
    public static void main(String args[])throws
                                   IOException
    {
      if(checkAbundant(12))
          System.out.println("YES");
      else
          System.out.println("NO");
      if(checkAbundant(15))
          System.out.println("YES");
      else
          System.out.println("NO");
    }
}
  
// This code is contributed by Nikita Tiwari.


Python
# An Optimized Solution to check Abundant Number
# in PYTHON
import math
 
# Function to calculate sum of divisors
def getSum(n) :
    sum = 0
     
    # 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
     
    # calculate sum of all proper divisors only
    sum = sum - n
    return sum
 
# Function to check Abundant Number
def checkAbundant(n) :
     
    # Return true if sum of divisors is greater
    # than n.
    if (getSum(n) > n) :
        return 1
    else :
        return 0
         
# Driver program to test above function */
if(checkAbundant(12) == 1) :
    print "YES"
else :
    print "NO"
     
if(checkAbundant(15) == 1) :
    print "YES"
else :
    print "NO"
     
# This code is contributed by Nikita Tiwari.


C#
// An Optimized Solution to check Abundant Number
// in C#
// Function to calculate sum of divisors
using System;
 
class GFG {
     
    // Function to calculate sum of divisors
    static int getSum(int n)
    {
        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);
                }
            }
        }
 
        // calculate sum of all proper divisors
        // only
        sum = sum - n;
        return sum;
    }
 
    // Function to check Abundant Number
    static bool checkAbundant(int n)
    {
         
        // Return true if sum of divisors is
        // greater than n.
        return (getSum(n) > n);
    }
 
    /* Driver program to test above function */
    public static void Main()
    {
        if (checkAbundant(12))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
             
        if (checkAbundant(15))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
 
// This code is contributed by vt_m.


PHP
 $n);
}
 
// Driver Code
$k = checkAbundant(12) ? "YES\n" : "NO\n";
echo($k);
 
$k = checkAbundant(15) ? "YES\n" : "NO\n";
echo($k);
 
// This code is contributed by Ajit.
?>


Javascript


输出 :

YES
NO