首页  >  文章  >  后端开发  >  在C编程中,将序列2、6、12、20、30的前N项求和

在C编程中,将序列2、6、12、20、30的前N项求和

WBOY
WBOY转载
2023-08-26 16:05:151181浏览

在C编程中,将序列2、6、12、20、30的前N项求和

要求这个级数的总和,我们首先分析这个级数。

该级数是:2,6,12,20,30…

示例

For n = 6
Sum = 112
On analysis, (1+1),(2+4),(3+9),(4+16)...
(1+1<sup>2</sup>), (2+2<sup>2</sup>), (3+3<sup>2</sup>), (4+4<sup>2</sup>), can be divided into two series i.e.
s1:1,2,3,4,5&hellip; andS2: 1<sup>2</sup>,<sup>2</sup>,3<sup>2</sup>,....

使用数学公式求出第一个和第二个之和

Sum1 = 1+2+3+4&hellip; , sum1 = n*(n+1)/2
Sum2 = 12+2<sup>2</sup>+3<sup>2</sup>+4<sup>2</sup>&hellip; , sum1 = n*(n+1)*(2*n +1)/6

示例

#include <stdio.h>
int main() {
   int n = 3;
   int sum = ((n*(n+1))/2)+((n*(n+1)*(2*n+1))/6);
   printf("the sum series till %d is %d", n,sum);
   return 0;
}

输出

The sum of series till 3 is 20

以上是在C编程中,将序列2、6、12、20、30的前N项求和的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除