📜  求自然数所有除数的除数之和

📅  最后修改于: 2021-04-29 11:47:27             🧑  作者: Mango

给定自然数n ,任务是找到n的所有除数的和。

例子:

Input : n = 54
Output : 232
Divisors of 54 = 1, 2, 3, 6, 9, 18, 27, 54.
Sum of divisors of 1, 2, 3, 6, 9, 18, 27, 54 
are 1, 3, 4, 12, 13, 39, 40, 120 respectively.
Sum of divisors of all the divisors of 54 = 
1 + 3 + 4 + 12 + 13 + 39 + 40 + 120 = 232.

Input : n = 10
Output : 28
Divisors of 10 are 1, 2, 5, 10
Sums of divisors of divisors are 
1, 3, 6, 18.
Overall sum = 1 + 3 + 6 + 18 = 28

利用任何数字n都可以表示为质数乘积的事实, n = p 1 k1 xp 2 k2 x…,其中p 1 ,p 2 ,…是质数。
n的所有除数可以表示为p 1 a xp 2 b x…,其中0 <= a <= k1和0 <= b <= k2。
现在,除数之和就是p 1 – p 1 0 ,p 1 1 ,…。,p 1 k1的所有幂的总和乘以p 2 – p 2 0 ,p 2 1 ,…。,p 2 k1的所有幂。
n除数之和
=(p 1 0 xp 2 0 )+(p 1 1 xp 2 0 )+….. +(p 1 k1 xp 2 0 )+…。+(p 1 0 xp 2 1 )+(p 1 1 xp 2 2 1 )+ ….. +(p 1 k1 xp 2 1 )+…….. +
(p 1 0 xp 2 k2 )+(p 1 1 xp 2 k2 )+……+(p 1 k1 xp 2 k2 )。
=(p 1 0 + p 1 1 +…+ p 1 k1 )xp 2 0 +(p 1 0 + p 1 1 +…+ p 1 k1 )xp 2 1 +……。+(p 1 0 + p 1 1 + … + p 1 k1 )xp 2 k2
=(p 1 0 + p 1 1 +…+ p 1 k1 )x(p 2 0 + p 2 1 +…+ p 2 k2 )。

现在,任何p a的除数(以p为素数)为p 0 ,p 1 ,……,p a 。除数之和为(p (a + 1) – 1)/(p -1),由f(p)定义。
因此,所有除数的除数之和为
=(f(p 1 0 )+ f(p 1 1 )+…+ f(p 1 k1 ))x(f(p 2 0 )+ f(p 2 1 )+…+ f(p 2 k2 )) 。

因此,给定一个数n,通过质因子分解,我们可以找到所有除数的除数之和。但是在这个问题上,我们得到n是数组元素的乘积。因此,通过使用a b xa c = a b + c的事实来找到每个元素的素因式分解。

以下是此方法的实现:

C++
// C++ program to find sum of divisors of all
// the divisors of a natural number.
#include
using namespace std;
  
// Returns sum of divisors of all the divisors
// of n
int sumDivisorsOfDivisors(int n)
{
    // Calculating powers of prime factors and
    // storing them in a map mp[].
    map mp;
    for (int j=2; j<=sqrt(n); j++)
    {
        int count = 0;
        while (n%j == 0)
        {
            n /= j;
            count++;
        }
  
        if (count)
            mp[j] = count;
    }
  
    // If n is a prime number
    if (n != 1)
        mp[n] = 1;
  
    // For each prime factor, calculating (p^(a+1)-1)/(p-1)
    // and adding it to answer.
    int ans = 1;
    for (auto it : mp)
    {
        int pw = 1;
        int sum = 0;
  
        for (int i=it.second+1; i>=1; i--)
        {
            sum += (i*pw);
            pw *= it.first;
        }
        ans *= sum;
    }
  
    return ans;
}
  
// Driven Program
int main()
{
    int n = 10;
    cout << sumDivisorsOfDivisors(n);
    return 0;
}


Java
// Java program to find sum of divisors of all 
// the divisors of a natural number. 
import java.util.HashMap;
  
class GFG 
{
  
    // Returns sum of divisors of all the divisors
    // of n
    public static int sumDivisorsOfDivisors(int n)
    {
  
        // Calculating powers of prime factors and
        // storing them in a map mp[].
        HashMap mp = new HashMap<>();
        for (int j = 2; j <= Math.sqrt(n); j++) 
        {
            int count = 0;
            while (n % j == 0) 
            {
                n /= j;
                count++;
            }
            if (count != 0)
                mp.put(j, count);
        }
  
        // If n is a prime number
        if (n != 1)
            mp.put(n, 1);
  
        // For each prime factor, calculating (p^(a+1)-1)/(p-1)
        // and adding it to answer.
        int ans = 1;
  
        for (HashMap.Entry entry : mp.entrySet()) 
        {
            int pw = 1;
            int sum = 0;
            for (int i = entry.getValue() + 1; i >= 1; i--)
            {
                sum += (i * pw);
                pw = entry.getKey();
            }
            ans *= sum;
        }
  
        return ans;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int n = 10;
        System.out.println(sumDivisorsOfDivisors(n));
    }
}
  
// This code is contributed by
// sanjeev2552


Python3
# Python3 program to find sum of divisors 
# of all the divisors of a natural number.
import math as mt
  
# Returns sum of divisors of all 
# the divisors of n
def sumDivisorsOfDivisors(n):
  
    # Calculating powers of prime factors 
    # and storing them in a map mp[].
    mp = dict()
    for j in range(2, mt.ceil(mt.sqrt(n))):
  
        count = 0
        while (n % j == 0):
            n //= j
            count += 1
  
        if (count):
            mp[j] = count
  
    # If n is a prime number
    if (n != 1):
        mp[n] = 1
  
    # For each prime factor, calculating 
    # (p^(a+1)-1)/(p-1) and adding it to answer.
    ans = 1
    for it in mp:
        pw = 1
        summ = 0
  
        for i in range(mp[it] + 1, 0, -1):
            summ += (i * pw)
            pw *= it
      
        ans *= summ
  
    return ans
  
# Driver Code
n = 10
print(sumDivisorsOfDivisors(n))
      
# This code is contributed
# by mohit kumar 29


C#
// C# program to find sum of divisors of all 
// the divisors of a natural number. 
using System;
using System.Collections.Generic; 
      
class GFG 
{
  
    // Returns sum of divisors of 
    // all the divisors of n
    public static int sumDivisorsOfDivisors(int n)
    {
  
        // Calculating powers of prime factors and
        // storing them in a map mp[].
        Dictionary mp = new Dictionary();
        for (int j = 2; j <= Math.Sqrt(n); j++) 
        {
            int count = 0;
            while (n % j == 0) 
            {
                n /= j;
                count++;
            }
            if (count != 0)
                mp.Add(j, count);
        }
  
        // If n is a prime number
        if (n != 1)
            mp.Add(n, 1);
  
        // For each prime factor, 
        // calculating (p^(a+1)-1)/(p-1)
        // and adding it to answer.
        int ans = 1;
  
        foreach(KeyValuePair entry in mp) 
        {
            int pw = 1;
            int sum = 0;
            for (int i = entry.Value + 1; 
                     i >= 1; i--)
            {
                sum += (i * pw);
                pw = entry.Key;
            }
            ans *= sum;
        }
  
        return ans;
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        int n = 10;
        Console.WriteLine(sumDivisorsOfDivisors(n));
    }
}
  
// This code is contributed
// by Princi Singh


输出:

28

优化:
对于需要输入多个值的情况,可以使用本文所讨论的Eratosthenes筛。