📜  直到 N 的所有整数与它们的除数的乘积之和

📅  最后修改于: 2021-10-26 05:09:30             🧑  作者: Mango

给定一个正整数N ,任务是找到[1, N]范围内所有整数与其除数数的乘积之和

例子:

朴素的方法:朴素的方法是从1 到 N遍历并找到所有数字的总和及其除数。

时间复杂度: O(N*sqrt(N))
辅助空间: O(1)

Efficient Approach:优化上述方式,思路是考虑总贡献 每个数字都能给出答案。以下是步骤:

  1. 对于[1, N]范围内的任何数字X ,X 对从 1 到 N 的每个 K 的总和贡献 K,使得K 是 X 的倍数
  2. 观察到这些K的列表的形式为i, 2i, 3i, …, Fi 其中 F 是 i 在1 和 N之间的倍数。
  3. 因此,要找到上面生成的这些数字的总和,由下式给出

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the sum of the
// product of all the integers and
// their positive divisors up to N
long long sumOfFactors(int N)
{
    long long ans = 0;
 
    // Iterate for every number
    // between 1 and N
    for (int i = 1; i <= N; i++) {
 
        // Find the first multiple
        // of i between 1 and N
        long long first = i;
 
        // Find the last multiple
        // of i between 1 and N
        long long last = (N / i) * i;
 
        // Find the total count of
        // multiple of in [1, N]
        long long factors
            = (last - first) / i + 1;
 
        // Compute the contribution of i
        // using the formula
        long long totalContribution
            = (((factors) * (factors + 1)) / 2) * i;
 
        // Add the contribution
        // of i to the answer
        ans += totalContribution;
    }
 
    // Return the result
    return ans;
}
 
// Driver code
int main()
{
    // Given N
    int N = 3;
 
    // function call
    cout << sumOfFactors(N);
}


Java
// Java program for the above approach
class GFG{
   
// Function to find the sum of the
// product of all the integers and
// their positive divisors up to N
static int sumOfFactors(int N)
{
    int ans = 0;
  
    // Iterate for every number
    // between 1 and N
    for (int i = 1; i <= N; i++)
    {
  
        // Find the first multiple
        // of i between 1 and N
        int first = i;
  
        // Find the last multiple
        // of i between 1 and N
        int last = (N / i) * i;
  
        // Find the total count of
        // multiple of in [1, N]
        int factors = (last - first) / i + 1;
  
        // Compute the contribution of i
        // using the formula
        int totalContribution = (((factors) *
                                  (factors + 1)) / 2) * i;
  
        // Add the contribution
        // of i to the answer
        ans += totalContribution;
    }
  
    // Return the result
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    // Given N
    int N = 3;
  
    // function call
    System.out.println(sumOfFactors(N));
}
}
 
// This code is contributed by Ritik Bansal


Python3
# Python3 implementation of
# the above approach
 
# Function to find the sum of the
# product of all the integers and
# their positive divisors up to N
def sumOfFactors(N):
 
    ans = 0
 
    # Iterate for every number
    # between 1 and N
    for i in range(1, N + 1):
 
        # Find the first multiple
        # of i between 1 and N
        first = i
 
        # Find the last multiple
        # of i between 1 and N
        last = (N // i) * i
 
        # Find the total count of
        # multiple of in [1, N]
        factors = (last - first) // i + 1
 
        # Compute the contribution of i
        # using the formula
        totalContribution = (((factors *
                              (factors + 1)) // 2) * i)
 
        # Add the contribution
        # of i to the answer
        ans += totalContribution
 
    # Return the result
    return ans
 
# Driver Code
 
# Given N
N = 3
 
# Function call
print(sumOfFactors(N))
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
class GFG{
   
// Function to find the sum of the
// product of all the integers and
// their positive divisors up to N
static int sumOfFactors(int N)
{
    int ans = 0;
  
    // Iterate for every number
    // between 1 and N
    for (int i = 1; i <= N; i++)
    {
  
        // Find the first multiple
        // of i between 1 and N
        int first = i;
  
        // Find the last multiple
        // of i between 1 and N
        int last = (N / i) * i;
  
        // Find the total count of
        // multiple of in [1, N]
        int factors = (last - first) / i + 1;
  
        // Compute the contribution of i
        // using the formula
        int totalContribution = (((factors) *
                                  (factors + 1)) / 2) * i;
  
        // Add the contribution
        // of i to the answer
        ans += totalContribution;
    }
  
    // Return the result
    return ans;
}
  
// Driver code
public static void Main(String[] args)
{
    // Given N
    int N = 3;
  
    // function call
    Console.WriteLine(sumOfFactors(N));
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
11

时间复杂度: O(N)
辅助空间: O(1)