系列2、6、12、20、30…的前N个项的总和。在C编程中

为了找到该系列的总和,我们将首先分析该系列。

该系列是:2,6,12,20,30…

示例

For n = 6
Sum = 112
On analysis, (1+1),(2+4),(3+9),(4+16)...
(1+12), (2+22), (3+32), (4+42), can be divided into two series i.e.
s1:1,2,3,4,5… andS2: 12,2,32,....

用数学公式求出第一和第二的总和

Sum1 = 1+2+3+4… , sum1 = n*(n+1)/2
Sum2 = 12+22+32+42… , 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