📜  查找前n个自然数的第m个求和。

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

前n个自然数的第m个求和定义如下。

If m > 1
  SUM(n, m) = SUM(SUM(n, m - 1), 1)
Else 
  SUM(n, 1) = Sum of first n natural numbers.

给定m和n,我们需要找到SUM(n,m)。
例子:

Input  : n = 4, m = 1 
Output : SUM(4, 1) = 10
Explanation : 1 + 2 + 3 + 4 = 10

Input  : n = 3, m = 2 
Output : SUM(3, 2) = 21
Explanation : SUM(3, 2) 
             = SUM(SUM(3, 1), 1) 
             = SUM(6, 1) 
             = 21

天真的方法:我们可以使用两个嵌套循环来解决此问题,其中,外循环迭代m,内循环迭代n。在完成一次外部迭代后,我们应在执行整个内部循环时更新n,然后必须更改n的值。时间复杂度应为O(n * m)。

for (int i = 1;i <= m;i++)
{
    sum = 0;
    for (int j = 1;j <= n;j++)
        sum += j;
    n = sum;  // update n
}

高效的方法:
我们可以使用直接公式计算前n个数字的总和以减少时间。
我们还可以使用递归。在这种方法中,m = 1将是我们的基本条件,对于任何中间步骤SUM(n,m),我们将称其为SUM(SUM(n,m-1),1),而对于单个步骤SUM(n,1) = n *(n + 1)/ 2。这将把我们的时间复杂度降低到O(m)。

int SUM (int n, int m)
{
    if (m == 1)
        return (n * (n + 1) / 2);
    int sum = SUM(n, m-1);
    return (sum * (sum + 1) / 2);
}

下面是上述想法的实现:

C++
// CPP program to find m-th summation
#include 
using namespace std;
 
// Function to return mth summation
int SUM(int n, int m)
{  
    // base case
    if (m == 1)
        return (n * (n + 1) / 2);
         
    int sum = SUM(n, m-1);
    return (sum * (sum + 1) / 2);
}
 
// driver program
int main()
{
    int n = 5;
    int m = 3;
    cout << "SUM(" << n << ", " << m
         << "): " << SUM(n, m);
    return 0;
}


Java
// Java program to find m-th summation.
class GFG {
     
    // Function to return mth summation
    static int SUM(int n, int m) {
         
        // base case
        if (m == 1)
            return (n * (n + 1) / 2);
     
        int sum = SUM(n, m - 1);
         
        return (sum * (sum + 1) / 2);
    }
     
    // Driver code
    public static void main(String[] args) {
         
        int n = 5;
        int m = 3;
         
        System.out.println("SUM(" + n + ", "
                        + m + "): "    + SUM(n, m));
    }
}
 
// This code is contributed by Anant Agarwal.
Python3 # Python3 program to find m-th summation 

# Function to return mth summation
def SUM(n, m):

    # base case
    if (m == 1):
        return (n * (n + 1) / 2)
        
    sum = SUM(n, m-1)
    return int(sum * (sum + 1) / 2)


# driver program
n = 5
m = 3
print("SUM(", n, ", ", m, "):", SUM(n, m))

# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to find m-th summation.
using System;
 
class GFG
{
     
    // Function to return mth summation
    static int SUM(int n, int m)
    {
         
        // base case
        if (m == 1)
            return (n * (n + 1) / 2);
     
        int sum = SUM(n, m - 1);
         
        return (sum * (sum + 1) / 2);
    }
     
    // Driver Code
    public static void Main()
    {
         
        int n = 5;
        int m = 3;
         
        Console.Write("SUM(" + n + ", "
                       + m + "): " + SUM(n, m));
    }
}
 
// This code is contributed by Nitin Mittal.


PHP


Javascript


输出:

SUM(5, 3): 7260