📌  相关文章
📜  系列3、20、63、144,……的总和

📅  最后修改于: 2021-05-04 13:42:35             🧑  作者: Mango

找到给定系列的前n个项的总和:

3, 20, 63, 144, .....

例子:

Input : n = 2
Output : 23

Input : n =4
Output : 230

方法
首先,我们必须找到给定序列的通用项(Tn)。

series can we written in the following way also:
(3 * 1^2), (5 * 2^2), (7 * 3^2), (9 * 4^2), .......up t n terms
Tn = (General term of series 3, 5, 7, 9 ....) X (General term of series 1^2, 2^2, 3^2, 4^2 ....)
Tn = (3 + (n-1) * 2) X ( n^2 )
Tn = 2*n^3 + n^2

我们可以通过以下方式写出序列的总和:

Sn = 3 + 20 + 63 + 144 + ……..直到n个项

    $$    Sn = \sum_{n=1}^{n} T_{n} $$

    $$      Sn = 2 \times \sum_{n=1}^{n} n^{3} + \sum_{n=1}^{n} n^{2}  $$

Sn = 2 *(n ^ 3的n项之和)+(n ^ 2的n项之和)

以下是n ^ 3和n ^ 2的n个项之和的公式:

    $$ \sum_{n=1}^{n} n^{3} = \left[\frac{n \times \big(n + 1 \big) }{2} \right]^{2} $$ $$ \sum_{n=1}^{n} n^{2} = \frac{n \times \big(n + 1 \big) \times \big(2*n + 1 \big) }{6} $$

Total = 2 \times \left[\frac{n \times \big(n + 1 \big) }{2} \right]^{2} + \frac{n \times \big(n + 1 \big) \times \big(2*n + 1 \big) }{6}
下面是上述方法的实现:

C++
// C++ program to find the sum of n terms
#include 
using namespace std;
int calculateSum(int n)
{
    return (2 * pow((n * (n + 1) / 2), 2)) +
           ((n * (n + 1) * (2 * n + 1)) / 6);
}
int main()
{
    int n = 4;
    cout << "Sum = " << calculateSum(n) << endl;
    return 0;
}


Java
// Java program to find the sum of n terms
import java.io.*;
 
public class GFG
{
    static int calculateSum(int n)
    {
        return (int)((2 * Math.pow((n * (n + 1) / 2), 2))) +
               ((n * (n + 1) * (2 * n + 1)) / 6);
    }
     
    public static void main (String[] args) {
     
        int n = 4;
        System.out.println("Sum = " +  calculateSum(n));
     
    }
}
// This code is contributed by Raj


Python3
# Python3 program to find the sum of n terms
 
def calculateSum(n):
    return ((2 * (n * (n + 1) / 2)**2) +
           ((n * (n + 1) * (2 * n + 1)) / 6))
     
#Driver code
 
n = 4
print("Sum =",calculateSum(n))
 
# this code is contributed by Shashank_Sharma


C#
// C# program to find the sum of n terms
using System;
 
class GFG
{
static int calculateSum(int n)
{
    return (int)((2 * Math.Pow((n * (n + 1) / 2), 2))) +
                     ((n * (n + 1) * (2 * n + 1)) / 6);
}
 
// Driver Code
public static void Main ()
{
    int n = 4;
    Console.WriteLine("Sum = " + calculateSum(n));
}
}
 
// This code is contributed by anuj_67


PHP


Javascript


输出:
Sum = 230