简体   繁体   English

一个数的所有因数的因数之和

[英]sum of divisors of all divisors of a number

The very well explanation of below approach is here .I was not able to write here due to formatting issues.以下方法的很好解释是here 。由于格式问题,我无法在这里写。

// C++ program to find sum of divisors of all the divisors of a natural number. // 计算一个自然数的所有因数的因数之和的 C++ 程序。

#include<bits/stdc++.h> 
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<int, int> 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; 
} 

I am not getting what is happening in this loop instead of adding to ans they are multiplying sum ,how they are calculating (p^(a+1)-1)/(p-1) and this to ans.can anyone help me with the intuition behind this loop.我没有得到这个循环中发生的事情,而不是添加到他们正在乘以总和的 ans,他们如何计算(p^(a+1)-1)/(p-1)和这个 ans.谁能帮助我用这个循环背后的直觉。

I got this from here我从这里得到这个

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; 
}

First consider this statement:首先考虑这个语句:

(p 1 0 + p 1 1 +…+ p 1 k 1 ) * (p 2 0 + p 2 1 +…+ p 2 k 2 ) (p 1 0 + p 1 1 +…+ p 1 k 1 ) * (p 2 0 + p 2 1 +…+ p 2 k 2 )

Now, the divisors of any p a , for p as prime, are p 0 , p 1 ,……, p a , and sum of diviors will be :现在,任何 p a的除数,对于 p 作为素数,是 p 0 , p 1 ,……, p a ,并且除数的总和将是:

((p 1 0 ) + (p 1 0 + p 1 1 ) + .... + (p 1 0 + p 1 1 + ...+ p k 1 )) * ((p 2 0 ) + (p 2 0 + p 2 1 ) + (p 2 0 + p 2 1 + p 2 2 ) + ... (p 2 0 + p 2 1 + p 2 2 + .. + p 2 k 2 )) ((p 1 0 ) + (p 1 0 + p 1 1 ) + .... + (p 1 0 + p 1 1 + ...+ p k 1 )) * ((p 2 0 ) + (p 2 0 + p 2 1 ) + (p 2 0 + p 2 1 + p 2 2 ) + ... (p 2 0 + p 2 1 + p 2 2 + .. + p 2 k 2 ))

you can consider the above statement equivalent to bellow statement:您可以将上述语句视为等效于以下语句:

[[p 1 0 * (k 1 + 1) + p 1 1 * k 1 + p 1 2 * (k 1 - 1 ) + .... + (p 1 k 1 * 1) ]] * [[p 2 0 * (k 2 + 1) + p 2 1 * (k 2 ) + p 2 2 * (k 2 - 1 ) + .... + (p 2 k 2 * 1) ]] in the code that you write in the post, the last statement was implemented. [[p 1 0 * (k 1 + 1) + p 1 1 * k 1 + p 1 2 * (k 1 - 1 ) + .... + (p 1 k 1 * 1) ]] * [[p 2 0 * (k 2 + 1) + p 2 1 * (k 2 ) + p 2 2 * (k 2 - 1 ) + .... + (p 2 k 2 * 1) ]] 在你的代码中写在帖子里,最后一句被执行了。

for example if you consider n = 54 = 3 3 * 2 1 ,例如,如果您考虑 n = 54 = 3 3 * 2 1
the ans is calculated in this format: ans 以这种格式计算:

ans = (2 0 * 2 + 2 1 * 1) * (3 0 * 4 + 3 1 * 3 + 3 2 * 2 + 3 3 *1) = 4 * 58 = 232 ans = (2 0 * 2 + 2 1 * 1) * (3 0 * 4 + 3 1 * 3 + 3 2 * 2 + 3 3 *1) = 4 * 58 = 232

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM