📌  相关文章
📜  系列的总和1.2.3 + 2.3.4 +…+ n(n + 1)(n + 2)

📅  最后修改于: 2021-04-29 05:32:09             🧑  作者: Mango

求出该系列的n个项之和:1.2.3 + 2.3.4 +…+ n(n + 1)(n + 2)。在此1.2.3代表第一项,而2.3.4代表第二项。
例子 :

Input : 2
Output : 30
1.2.3 + 2.3.4 = 6 + 24 = 30

Input : 3
Output : 90

简单方法我们对i = 1到n进行循环,并找到(i)*(i + 1)*(i + 2)之和。
最后显示总和。

C++
// CPP program to find sum of the series
// 1.2.3 + 2.3.4 + 3.4.5 + ...
#include 
using namespace std;
 
int sumofseries(int n)
{
    int res = 0;
    for (int i = 1; i <= n; i++)
        res += (i) * (i + 1) * (i + 2);   
    return res;
}
 
// Driver Code
int main()
{
    cout << sumofseries(3) << endl;
    return 0;
}


Java
// Java program to find sum of the series
// 1.2.3 + 2.3.4 + 3.4.5 + ...
import java.io.*;
import java.math.*;
 
class GFG
{
 
    static int sumofseries(int n)
    {
    int res = 0;
    for (int i = 1; i <= n; i++)
        res += (i) * (i + 1) * (i + 2);
    return res;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println(sumofseries(3));
    }
}


Python3
# Python 3 program to find sum of the series
# 1.2.3 + 2.3.4 + 3.4.5 + ...
 
def sumofseries(n):
 
    res = 0
    for i in range(1, n+1):
        res += (i) * (i + 1) * (i + 2)
    return res
 
# Driver Program
print(sumofseries(3))
 
# This code is contributed
# by Smitha Dinesh Semwal


C#
// Java program to find sum of the series
// 1.2.3 + 2.3.4 + 3.4.5 + ...
using System;
 
class GFG
{
 
    static int sumofseries(int n)
    {
        int res = 0;
        for (int i = 1; i <= n; i++)
            res += (i) * (i + 1) * (i + 2);
        return res;
    }
 
    // Driver Code
    public static void Main()
    {
        Console.WriteLine(sumofseries(3));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// Efficient CPP program to
// find sum of the series
// 1.2.3 + 2.3.4 + 3.4.5 + ...
#include 
using namespace std;
 
// function to calculate
// sum of series
int sumofseries(int n)
{
    return (n * (n + 1) *
           (n + 2) * (n + 3) / 4);
}
 
// Driver Code
int main()
{
    cout << sumofseries(3) << endl;
    return 0;
}


Java
// Efficient Java program to
// find sum of the series
// 1.2.3 + 2.3.4 + 3.4.5 + ..
import java.io.*;
import java.math.*;
 
class GFG
{
    static int sumofseries(int n)
    {
    return (n * (n + 1) *
           (n + 2) * (n + 3) / 4);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println(sumofseries(3));
    }
}


Python3
# Efficient CPP program to find sum of the
# series 1.2.3 + 2.3.4 + 3.4.5 + ...
 
# function to calculate sum of series
def sumofseries(n):
 
    return int(n * (n + 1) * (n + 2) * (n + 3) / 4)
 
 
# Driver program
print(sumofseries(3))
     
 
# This code is contributed
# by Smitha Dinesh Semwal


C#
// Efficient C# program to
// find sum of the series
// 1.2.3 + 2.3.4 + 3.4.5 + ..
using System;
 
class GFG
{
    static int sumofseries(int n)
    {
    return (n * (n + 1) *
           (n + 2) * (n + 3) / 4);
    }
 
    // Driver Code
    public static void Main()
    {
        Console.WriteLine(sumofseries(3));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :
90
Complexity : O(N)

高效方法

使用有效方法,我们知道我们必须找到=(((n)*(n + 1)*(n + 2))的总和

C++

// Efficient CPP program to
// find sum of the series
// 1.2.3 + 2.3.4 + 3.4.5 + ...
#include 
using namespace std;
 
// function to calculate
// sum of series
int sumofseries(int n)
{
    return (n * (n + 1) *
           (n + 2) * (n + 3) / 4);
}
 
// Driver Code
int main()
{
    cout << sumofseries(3) << endl;
    return 0;
}

Java

// Efficient Java program to
// find sum of the series
// 1.2.3 + 2.3.4 + 3.4.5 + ..
import java.io.*;
import java.math.*;
 
class GFG
{
    static int sumofseries(int n)
    {
    return (n * (n + 1) *
           (n + 2) * (n + 3) / 4);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println(sumofseries(3));
    }
}

Python3

# Efficient CPP program to find sum of the
# series 1.2.3 + 2.3.4 + 3.4.5 + ...
 
# function to calculate sum of series
def sumofseries(n):
 
    return int(n * (n + 1) * (n + 2) * (n + 3) / 4)
 
 
# Driver program
print(sumofseries(3))
     
 
# This code is contributed
# by Smitha Dinesh Semwal

C#

// Efficient C# program to
// find sum of the series
// 1.2.3 + 2.3.4 + 3.4.5 + ..
using System;
 
class GFG
{
    static int sumofseries(int n)
    {
    return (n * (n + 1) *
           (n + 2) * (n + 3) / 4);
    }
 
    // Driver Code
    public static void Main()
    {
        Console.WriteLine(sumofseries(3));
    }
}
 
// This code is contributed by anuj_67.

的PHP


Java脚本


输出 :
90

时间复杂度: O(1)