给定一个正整数n ,任务是找到级数1 * 2 * 3 + 2 * 3 * 4 + 4 * 5 * 6 +的总和。 。 。+ n *(n + 1)*(n + 2)。
例子:
Input : n = 10
Output : 4290
1*2*3 + 2*3*4 + 3*4*5 + 4*5*6 + 5*6*7 + 6*7*8 +
7*8*9 + 8*9*10 + 9*10*11 + 10*11*12
= 6 + 24 + 60 + 120 + 210 + 336 + 504 +
720 + 990 + 1320
= 4290
Input : n = 7
Output : 1260
方法1:在这种情况下,循环将运行n次并计算总和。
C++
// Program to find the sum of series
// 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1)
#include
using namespace std;
// Function to calculate sum of series.
int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i * (i + 1) * (i + 2);
return sum;
}
// Driver function
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
Java
// Java Program to find the sum of series
// 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1)
public class GfG{
// Function to calculate sum of series.
static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i * (i + 1) * (i + 2);
return sum;
}
// Driver Code
public static void main(String s[])
{
int n = 10;
System.out.println(sumOfSeries(n));
}
}
// This article is contributed by Gitanjali.
Python3
# Python program to find the
# sum of series
# 1*2*3 + 2*3*4 + . . .
# + n*(n+1)*(n+1)
# Function to calculate sum
# of series.
def sumOfSeries(n):
sum = 0;
i = 1;
while i<=n:
sum = sum + i * (i + 1) * (
i + 2)
i = i + 1
return sum
# Driver code
n = 10
print(sumOfSeries(n))
# This code is contributed by "Abhishek Sharma 44"
C#
// C# Program to find the sum of series
// 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1)
using System;
public class GfG
{
// Function to calculate sum of series.
static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i * (i + 1) * (i + 2);
return sum;
}
// Driver Code
public static void Main()
{
int n = 10;
Console.WriteLine(sumOfSeries(n));
}
}
// This article is contributed by vt_m.
PHP
Javascript
C++
// Program to find the sum of series
// 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1)
#include
using namespace std;
// Function to calculate sum of series.
int sumOfSeries(int n)
{
return (n * (n + 1) * (n + 2) * (n + 3)) / 4;
}
// Driver function
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
Java
// Program to find the
// sum of series
// 1*2*3 + 2*3*4 +
// . . . + n*(n+1)*(n+1)
import java.io.*;
class GFG {
// Function to calculate
// sum of series.
static int sumOfSeries(int n)
{
return (n * (n + 1) *
(n + 2) * (n + 3)) / 4;
}
// Driver function
public static void main (String[] args) {
int n = 10;
System.out.println(sumOfSeries(n));
}
}
// This code is contributed by Nikita Tiwari.
Python3
# Python program to find the
# sum of series
# 1*2*3 + 2*3*4 + . . .
# + n*(n+1)*(n+1)
# Function to calculate sum
# of series.
def sumOfSeries(n):
return (n * (n + 1) * (n + 2
) * (n + 3)) / 4
#Driver code
n = 10
print(sumOfSeries(n))
# This code is contributed by "Abhishek Sharma 44"
C#
// Program to find the
// sum of series
// 1*2*3 + 2*3*4 +
// . . . + n*(n+1)*(n+1)
using System;
class GFG {
// Function to calculate
// sum of series.
static int sumOfSeries(int n)
{
return (n * (n + 1) *
(n + 2) * (n + 3)) / 4;
}
// Driver function
public static void Main () {
int n = 10;
Console.WriteLine(sumOfSeries(n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
4290
时间复杂度:O(n)
方法2:在这种情况下,我们使用公式将序列的总和相加。
Given series 1*2*3 + 2*3*4 + 3*4*5 + 4*5*6 + . . . + n*(n+1)*(n+2)
sum of series = (n * (n+1) * (n+2) * (n+3)) / 4
Put n = 10 then
sum = (10 * (10+1) * (10+2) * (10+3)) / 4
= (10 * 11 * 12 * 13) / 4
= 4290
C++
// Program to find the sum of series
// 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1)
#include
using namespace std;
// Function to calculate sum of series.
int sumOfSeries(int n)
{
return (n * (n + 1) * (n + 2) * (n + 3)) / 4;
}
// Driver function
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
Java
// Program to find the
// sum of series
// 1*2*3 + 2*3*4 +
// . . . + n*(n+1)*(n+1)
import java.io.*;
class GFG {
// Function to calculate
// sum of series.
static int sumOfSeries(int n)
{
return (n * (n + 1) *
(n + 2) * (n + 3)) / 4;
}
// Driver function
public static void main (String[] args) {
int n = 10;
System.out.println(sumOfSeries(n));
}
}
// This code is contributed by Nikita Tiwari.
Python3
# Python program to find the
# sum of series
# 1*2*3 + 2*3*4 + . . .
# + n*(n+1)*(n+1)
# Function to calculate sum
# of series.
def sumOfSeries(n):
return (n * (n + 1) * (n + 2
) * (n + 3)) / 4
#Driver code
n = 10
print(sumOfSeries(n))
# This code is contributed by "Abhishek Sharma 44"
C#
// Program to find the
// sum of series
// 1*2*3 + 2*3*4 +
// . . . + n*(n+1)*(n+1)
using System;
class GFG {
// Function to calculate
// sum of series.
static int sumOfSeries(int n)
{
return (n * (n + 1) *
(n + 2) * (n + 3)) / 4;
}
// Driver function
public static void Main () {
int n = 10;
Console.WriteLine(sumOfSeries(n));
}
}
// This code is contributed by vt_m.
的PHP
Java脚本
输出:
4290
时间复杂度:O(1)
这个公式如何运作?
We can prove working of this formula using
mathematical induction.
According to formula, sum of (k -1) terms is
((k - 1) * (k) * (k + 1) * (k + 2)) / 4
Sum of k terms
= sum of k-1 terms + value of k-th term
= ((k - 1) * (k) * (k + 1) * (k + 2)) / 4 +
k * (k + 1) * (k + 2)
Taking common term (k + 1) * (k + 2) out.
= (k + 1)*(k + 2) [k*(k-1)/4 + k]
= (k + 1)*(k + 2) * k * (k + 3)/4
= k * (k + 1) * (k + 2) * (k + 3)/4