5,12、23、38…系列的前N个项的总和。在C编程中

为了找到给定系列的总和,我们将分析该系列并尝试获得一些特征,以表明该系列是已知系列,或者至少是2-3系列的组合。给定的序列是5、12、23、38…

我们必须找到任何n值的序列之和

例如

For n = 3
Sum = 40.

在分析给定序列时,您会发现该序列是二次序列。在二次级数中,数字的差是算术级数(按确定的数字增加)

因此,我们可以直接将公式用于二次序列的和。该级数之和的公式为:

Sum = (2*(n*(n+1)*(2*(n+1))/6))+n*(n+1)/2+2*n

示例

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

输出结果

the sum of series till 6 is 215