给定整数N ,任务是找到该系列的前N个项的总和:
(2 * 3 * 5), (3 * 5 * 7), (4 * 7 * 9), …
例子:
Input: N = 3
Output: 387
S3 = (2 * 3 * 5) + (3 * 5 * 7) + (4 * 7 * 9) = 30 + 105 + 252 = 387
Input: N = 5
Output: 1740
方法:让级数的第N个为T n 。通过观察序列的第N个项,可以很容易地找到该序列的总和:
Tn = {nth term of 2, 3, 4, …} * {nth term of 3, 5, 7, …} * {nth term of 5, 7, 9, …}
Tn = (n + 1) * (2 * n + 1) * (2* n + 3)
Tn = 4n3 + 12n2 + 11n + 3
前n个项的和( S n )可以通过
Sn = ΣTn
Sn = Σ[4n3 + 12n2 + 11n + 3]
Sn = (n / 2) * [2n3 + 12n2 + 25n + 21]
下面是上述方法的实现:
C++
// C++ program to find sum of the
// first n terms of the given series
#include
using namespace std;
// Function to return the sum of the
// first n terms of the given series
int calSum(int n)
{
// As described in the approach
return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2;
}
// Driver code
int main()
{
int n = 3;
cout << calSum(n);
return 0;
}
Java
// Java program to find sum of the
// first n terms of the given series
class GFG {
// Function to return the sum of the
// first n terms of the given series
static int calSum(int n)
{
// As described in the approach
return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2;
}
// Driver Code
public static void main(String args[])
{
int n = 3;
System.out.println(calSum(n));
}
}
Python
# C++ program to find sum of the
# first n terms of the given series
# Function to return the sum of the
# first n terms of the given series
def calSum(n):
# As described in the approach
return (n*(2 * n*n * n + 12 * n*n + 25 * n + 21))/2;
# Driver Code
n = 3
print(calSum(n))
C#
// C# program to find sum of the
// first n terms of the given series
using System;
class GFG {
// Function to return the sum of the
// first n terms of the given series
static int calSum(int n)
{
// As described in the approach
return (n * (2 * n * n * n + 12 * n * n + 25 * n + 21)) / 2;
}
// Driver code
static public void Main()
{
int n = 3;
Console.WriteLine(calSum(n));
}
}
PHP
Javascript
输出:
387