📌  相关文章
📜  求出系列2×3 + 4×4 + 6×5 + 8×6 +的前N个项的总和。

📅  最后修改于: 2021-04-23 20:21:47             🧑  作者: Mango

给定整数N。任务是找到高达给出一系列的N项之和

例子:

Input : N = 5
Output : Sum = 170

Input : N = 10
Output : Sum = 990

令级数的第N个t N。
t 1 = 2×3 =(2×1)(1 + 2)
t 2 = 4×4 =(2×2)(2 + 2)
t 3 = 6×5 =(2×3)(3 + 2)
t 4 = 8×6 =(2×4)(4 + 2)



t N =(2×N)(N + 2)
该系列的n个项之和,

S n = t 1 + t 2 + … + t n = \;\sum_{k=1}^{n} t_{k} = \;\sum_{k=1}^{n} 2k(k+2) = \;\sum_{k=1}^{n} (2k^{2}+4k) = \;\sum_{k=1}^{n}  2k^{2} + \;\sum_{k=1}^{n}  4k = 2\frac{n(n+1)(2n+1)}{6} + 4\frac{n(n+1)}{2} = n(n+1)[ \frac{2n+1}{3} +2] = \frac{n(n+1)(2n+7)}{3}

下面是上述方法的实现:

C++
// C++ program to find sum upto
// N term of the series:
// 2 × 3 + 4 × 4 + 6 × 5 + 8 × 6 + ...
#include
using namespace std;
 
// calculate sum upto N term of series
void Sum_upto_nth_Term(int n)
{
    int r = n * (n + 1) *
                (2 * n + 7) / 3;
    cout << r;
}
 
// Driver code
int main()
{
    int N = 5;
    Sum_upto_nth_Term(N) ;
    return 0;
}


Java
// Java program to find sum upto
// N term of the series:
 
import java.io.*;
 
class GFG {
// calculate sum upto N term of series
static void Sum_upto_nth_Term(int n)
{
    int r = n * (n + 1) *
                (2 * n + 7) / 3;
    System.out.println(r);
}
 
// Driver code
    public static void main (String[] args) {
    int N = 5;
    Sum_upto_nth_Term(N);
    }
}


Python3
# Python program to find sum upto N term of the series:
# 2 × 3 + 4 × 4 + 6 × 5 + 8 × 6 + ...
 
# calculate sum upto N term of series
def Sum_upto_nth_Term(n):
    return n * (n + 1) * (2 * n + 7) // 3
 
# Driver code
N = 5
print(Sum_upto_nth_Term(N))


C#
// C# program to find sum upto
// N term of the series:
// 2 × 3 + 4 × 4 + 6 × 5 + 8 × 6 + ...
 
using System;
 
class GFG
{
// calculate sum upto N term of series
static void Sum_upto_nth_Term(int n)
{
    int r = n * (n + 1) *
                (2 * n + 7) / 3;
    Console.Write(r);
}
 
// Driver code
public static void Main()
{
    int N = 5;
    Sum_upto_nth_Term(N);
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
170