Home >Backend Development >C++ >Translate the following into Chinese: Solving the sum of the sequence 1.2.3 + 2.3. + ... + n(n+1)(n+2) in C

Translate the following into Chinese: Solving the sum of the sequence 1.2.3 + 2.3. + ... + n(n+1)(n+2) in C

王林
王林forward
2023-09-13 22:37:021094browse

将以下内容翻译为中文:在C中求解序列1.2.3 + 2.3. + ... + n(n+1)(n+2)的和

Find the sum of n terms of the series: 1.2.3 2.3.4 … n(n 1)(n 2). Among them, 1.2.3 represents the first item and 2.3.4 represents the second item.

Let us see an example to understand this concept better,

Input: n = 5
Output: 420

Explanation

1.2.3 2.3.4 3.4.5 4.5.6 5.6.7 = 6 24 60 120 210 = 420

n items = n(n 1)(n 2);where n = 1,2,3,…

= n(n^2 3n 2) =n^3 3n^2 2n

Now, note that p>

summation=n(n 1)/2; if the nth item=n

=n(n 1)(2n 1)/6; if nth item=n^2

=n^2(n 1)^2/4; if nth item=n^3

Therefore the required sum =

n^2(n 1)^2 /4 3 ×n(n 1)(2n 1)/6 2 × n(n 1)/2

=n^2 (n 1)^2 /4 n(n 1)(2n 1)/2 n(n 1)

=n(n 1) { n(n 1)/4 ( 2n 1)/2 1 }

=n( n 1) { (n^2 n 4n 2 4)/4}

=1/4 n(n 1){ n^ 2 5n 6}

=1/4 n(n 1)(n 2)(n 3)

There are two ways to solve this problem,

One is using mathematical formulas and the other is looping.

In Mathematical Formula Method, the series summation formula of this series is given.

Algorithm

Input: n number of elements.

Step 1 : calc the sum,
   sum = 1/4{n(n+1)(n+2)(n+3)}
Step 2 : Print sum, using standard print method.

Example

Real-time demonstration

#include <stdio.h>
#include<math.h>
int main() {
   float n = 6;
   float area = n*(n+1)*(n+2)*(n+3)/4;
   printf("The sum is : %f",area);
   return 0;
}

Output

The sum is : 756

Example

Real-time demonstration

#include <stdio.h>
#include<math.h>
int main() {
   float n = 6;
   int res = 0;
   for (int i = 1; i <= n; i++)
      res += (i) * (i + 1) * (i + 2);
   printf("The sum is : %d",res);
   return 0;
}

Output

The sum is : 756

The above is the detailed content of Translate the following into Chinese: Solving the sum of the sequence 1.2.3 + 2.3. + ... + n(n+1)(n+2) in C. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete