Home > Backend Development > C++ > body text

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

王林
Release: 2023-09-13 22:37:02
forward
1058 people have browsed it

将以下内容翻译为中文:在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
Copy after login

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

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.
Copy after login

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;
}
Copy after login

Output

The sum is : 756
Copy after login
Copy after login

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;
}
Copy after login

Output

The sum is : 756
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!