📌  相关文章
📜  自然数的所有适当除数的总和

📅  最后修改于: 2021-04-26 05:32:32             🧑  作者: Mango

给定自然数,计算其所有适当除数的总和。自然数的适当除数是严格小于该数字的除数。
例如,数字20具有5个适当的除数:1、2、4、5、10,除数的总和为:1 + 2 + 4 + 5 + 10 = 22。
例子 :

Input : num = 10
Output: 8
// proper divisors 1 + 2 + 5 = 8 

Input : num = 36
Output: 55
// proper divisors 1 + 2 + 3 + 4 + 6 + 9 + 12 + 18 = 55 

这个问题的解决方案非常简单,我们都知道,对于任何数字’num’,其所有除数始终小于和等于’num / 2’,并且所有素数始终小于和等于sqrt(num) 。因此,我们遍历“ i”直到i <= sqrt(num),对于任何“ i”如果将其除以“ num”,则得到两个除数“ i”和“ num / i”,连续添加这些除数,但对于某些除数在这种情况下,数字除数’i’和’num / i’将相同,仅添加一个除数,例如; num = 36,所以对于i = 6,我们将得到(num / i)= 6,这就是为什么我们在6的总和中只会出现一次。最后,我们将其加一,因为它是所有自然数的除数。

C++
// C++ program to find sum of all divisors of
// a natural number
#include
using namespace std;
 
// Function to calculate sum of all proper divisors
// num --> given natural number
int divSum(int num)
{
    // Final result of summation of divisors
    int result = 0;
 
    // 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))
                result += i;
            else
                result += (i + num/i);
        }
    }
 
    // Add 1 to the result as 1 is also a divisor
    return (result + 1);
}
 
// Driver program to run the case
int main()
{
    int num = 36;
    cout << divSum(num);
    return 0;
}


Java
// JAVA program to find sum of all divisors
// of a natural number
import java.math.*;
 
class GFG {
     
    // Function to calculate sum of all proper
    // divisors num --> given natural number
    static int divSum(int num)
    {
        // Final result of summation of divisors
        int result = 0;
      
        // 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))
                    result += i;
                else
                    result += (i + num / i);
            }
        }
      
        // Add 1 to the result as 1 is also
        // a divisor
        return (result + 1);
    }
      
    // Driver program to run the case
    public static void main(String[] args)
    {
        int num = 36;
        System.out.println(divSum(num));
    }
}
 
/*This code is contributed by Nikita Tiwari*/


Python
# PYTHON program to find sum of all
# divisors of a natural number
import math
     
# Function to calculate sum of all proper
# divisors num --> given natural number
def divSum(num) :
     
    # Final result of summation of divisors
    result = 0
     
    # find all divisors which divides 'num'
    i = 2
    while i<= (math.sqrt(num)) :
       
        # 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)) :
                result = result + i;
            else :
                result = result +  (i + num/i);
        i = i + 1
         
    # Add 1 to the result as 1 is also
    # a divisor
    return (result + 1);
  
# Driver program to run the case
num = 36
print (divSum(num))
 
# This code is contributed by Nikita Tiwari


C#
// C# program to find sum of all
// divisorsof a natural number
using System;
 
class GFG {
     
    // Function to calculate sum of all proper
    // divisors num --> given natural number
    static int divSum(int num)
    {
         
        // Final result of summation of divisors
        int result = 0;
     
        // 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))
                    result += i;
                else
                    result += (i + num / i);
            }
        }
     
        // Add 1 to the result as 1
        // is also a divisor
        return (result + 1);
    }
     
    // Driver Code
    public static void Main()
    {
        int num = 36;
        Console.Write(divSum(num));
    }
}
 
// This code is contributed by Nitin Mittal.


PHP
 given natural number
function divSum($num)
{
    // Final result of
    // summation of divisors
    $result = 0;
 
    // find all divisors
    // which divides 'num'
    for ($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))
                $result += $i;
            else
                $result += ($i + $num / $i);
        }
    }
 
    // Add 1 to the result as
    // 1 is also a divisor
    return ($result + 1);
}
 
// Driver Code
$num = 36;
echo(divSum($num));
 
// This code is contributed by Ajit.
?>


Javascript


输出 :

55