📌  相关文章
📜  在给定序列的第n行中找到所有项的总和

📅  最后修改于: 2021-04-29 07:55:55             🧑  作者: Mango

在下面给出的系列的第n行中找到所有项的总和。

1  2
            3  4  5  6
         7  8  9 10 11 12
     13 14 15 16 17 18 19 20
    ..........................
   ............................
             (so on)

例子:

Input : n = 2
Output : 18
terms in 2nd row and their sum
sum = (3 + 4 + 5 + 6) = 18

Input : n = 4
Output : 132

天真的方法:使用两个循环。外循环执行i = 1到n次。内循环执行j = 1到2 * i次。计数器变量k跟踪序列中的当前项。当i = n时k的值累加到总和。
时间复杂度:O(k),其中k是从第n行的开始到结束的项的总数。
高效的方法:第n行中所有项的总和可通过以下公式获得:

Sum(n) = n * (2 * n2 + 1)

公式的证明如下:
先决条件:

  1. 算术级数序列的n个项之和,其中a为第一项, d为共同差,给出为:
Sum = (n * [2*a + (n-1)*d]) / 2
  1. n个自然数之和为:
Sum = (n * (n + 1)) / 2

证明:

Let the number of terms from the beginning 
till the end of the nth row be p.
Here p = 2 + 4 + 6 + .....n terms
For the given AP series, a = 2, d = 2.
Using the above formula for the sum of
n terms of the AP series, we get,

     p = n * (n + 1)

Similarly, let the number of terms from the 
beginning till the end of the (n-1)th row be q.
Here q = 2 + 4 + 6 + .....n-1 terms
For the given AP series, a = 2, d = 2.
Using the above formula for the sum of
n-1 terms of the AP series, we get,

     q = n * (n - 1)

Now,
Sum of all the terms in the nth row 
           = sum of 1st p natural numbers - 
             sum of 1st q natural numbers
    
           = (p * (p + 1)) / 2 - (q * (q + 1)) / 2

Substituting the values of p and q and then solving
the equation, we will get,

Sum of all the terms in the nth row = n * (2 * n2 + 1)
C++
// C++ implementation to find the sum of all the
// terms in the nth row of the given series
#include 
 
using namespace std;
 
// function to find the required sum
int sumOfTermsInNthRow(int n)
{
    // sum = n * (2 * n^2 + 1)
    int sum = n * (2 * pow(n, 2) + 1);
    return sum;
}
 
// Driver program to test above
int main()
{
    int n = 4;
    cout << "Sum of all the terms in nth row = "
         << sumOfTermsInNthRow(n);
    return 0;
}


Java
// Java implementation to find the sum of all the
// terms in the nth row of the given series
 
import static java.lang.Math.pow;
 
class Test {
    // method to find the required sum
    static int sumOfTermsInNthRow(int n)
    {
        // sum = n * (2 * n^2 + 1)
        int sum = (int)(n * (2 * pow(n, 2) + 1));
        return sum;
    }
 
    // Driver method
    public static void main(String args[])
    {
        int n = 4;
        System.out.println("Sum of all the terms in nth row = "
                           + sumOfTermsInNthRow(n));
    }
}


Python3
# Python 3 implementation to find
# the sum of all the terms in the
# nth row of the given series
from math import pow
 
# function to find the required sum
def sumOfTermsInNthRow(n):
     
    # sum = n * (2 * n^2 + 1)
    sum = n * (2 * pow(n, 2) + 1)
    return sum
 
# Driver Code
if __name__ == '__main__':
    n = 4
    print("Sum of all the terms in nth row =",
                   int(sumOfTermsInNthRow(n)))
 
# This code is contributed
# by Surendra_Gangwar


C#
// C# implementation to find the sum of all the
// terms in the nth row of the given series
using System;
 
class Test {
    // method to find the required sum
    static int sumOfTermsInNthRow(int n)
    {
        // sum = n * (2 * n^2 + 1)
        int sum = (int)(n * (2 * Math.Pow(n, 2) + 1));
        return sum;
    }
 
    // Driver method
    public static void Main()
    {
        int n = 4;
        Console.Write("Sum of all the terms in nth row = "
                      + sumOfTermsInNthRow(n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

Sum of all the terms in nth row = 132