📜  等分和

📅  最后修改于: 2021-04-29 02:32:42             🧑  作者: Mango

在数论中,正整数n的等分和s(n)是n的所有适当除数的和,即n本身以外的所有n的除数。
它们由等份除数之和定义。数字的等份除数是除数字本身以外的所有除数。等分和是等分因数的总和,因此,例如,等分因数12为1、2、3、4和6,等分因数为16。
等分和等于其值的数字是“完美”数字(例如6)。
例子 :

Input : 12
Output : 16
Explanation :
Proper divisors of 12 is = 1, 2, 3, 4, 6 
and sum 1 + 2 + 3 + 4 + 6 = 16

Input : 15
Output : 9
Explanation :
Proper divisors of 15 is 1, 3, 5
and sum 1 + 3 + 5 = 9

一个简单的解决方案是遍历所有小于n的数字。对于每个数字i,检查我是否除以n。如果是,我们将其添加到结果中。

C++
// CPP program for aliquot sum
#include 
using namespace std;
 
// Function to calculate sum of
// all proper divisors
int aliquotSum(int n)
{
    int sum = 0;
    for (int i = 1; i < n; i++)
        if (n % i == 0)
            sum += i;       
     
    return sum;
}
 
// Driver Code
int main()
{
    int n = 12;
    cout << aliquotSum(n);
    return 0;
}


Java
// Java program for aliquot sum
import java.io.*;
 
class GFG {
     
    // Function to calculate sum of
    // all proper divisors
    static int aliquotSum(int n)
    {
        int sum = 0;
        for (int i = 1; i < n; i++)
            if (n % i == 0)
                sum += i;
                 
        return sum;
    }
     
    // Driver Code
    public static void main(String args[])
                           throws IOException
    {
        int n = 12;
        System.out.println(aliquotSum(n));
    }
}
 
/* This code is contributed by Nikita Tiwari.*/


Python3
# Python 3 program for aliquot sum
 
# Function to calculate sum of
# all proper divisors
def aliquotSum(n) :
    sm = 0
    for i in range(1,n) :
        if (n % i == 0) :
            sm = sm + i    
     
    return sm # return sum
 
 
# Driver Code
n = 12
print(aliquotSum(n))
 
# This code is contributed by Nikita Tiwari.


C#
// C# program for aliquot sum
using System;
 
class GFG {
     
    // Function to calculate sum of
    // all proper divisors
    static int aliquotSum(int n)
    {
        int sum = 0;
        for (int i = 1; i < n; i++)
            if (n % i == 0)
                sum += i;
                 
        return sum;
    }
     
    // Driver Code
    public static void Main()
                         
    {
        int n = 12;
        Console.WriteLine(aliquotSum(n));
    }
}
 
/* This code is contributed by vt_m.*/


PHP


Javascript


输出 :

16

高效的解决方案:
自然数的所有适当除数的总和
一个数字的所有因素之和