# Sum of n terms of a sequence

If nth term of a sequence is given by Tn=an3+bn2+cn+d where a, b, c, d are constants then sum of n terms.

`Sn = a*Σ(n3)+b*Σ(n2)+c*Σ(n)+Σ(d) `

where Σ represent summation.

Proof :
Let us find some of this series. So,

```Sn = T1+T2+T3+T4+ ... +Tn
T1 = a(1)3+b(1)2+c(1)+d
T2 = a(2)3+b(2)2+c(2)+d
T3 = a(3)3+b(3)2+c(3)+d
...
...
Tn = a(n)3+b(n)3+c(n)+d ```

```Sn
= T1+T2+T3+ ... +Tn
= a((1)3+(2)3+(3)3+ ...
+(n)3)+b((1)2+(2)2+(3)2+ ...
+(n)2)+c(1+2+3+ ... +n)+d(1+1+1+ ... +1) ```
`Sn = a*Σ(n3)+b*Σ(n2)+c*Σ(n)+dn `

Similarly if we have been nth term for any higher order or lower order term in the format.

```Tn = a1np+a2np-1+a3np-2++
... +apn1+ap+1```
```Sn = a1Σ(np)+a2Σ(np-1)+a3Σ(np-2)+
... +apΣ(n)+nap+1```

where p, a1, a2, …… are some constants.

Example :
nth term is given as,

`Tn = n2+n+1 `

Calculate Sn

Explanation :

```Sn = Σ(Tn)
Sn = Σ(n2)+Σ(n)+Σ(1)
Sn = (n(n+1)(2n+1))/6+n(n+1)/2+n ```

Because,

```Σ(n2) = (n(n+1)(2n+1))/6,
Σ(n) = (n(n+1))/2,
Σ(1) = n ```

Thus we can find sum of any sequence if its nth term is given. This article is helpful in finding time complexity of equations when time complexity is given as function of n and we have to find the time complexity for the whole algorithm.

 `#include ` `using` `namespace` `std; ` `int` `main() ` `{ ` `    ``int` `k = 3; ` `    ``int` `sum = 0; ` `    ``// finding sum of n^4 terms ` `    ``for` `(``int` `i = 1; i <= k; i++) ` `        ``sum += (i * i * i * i); ` `    ``// sum of first k natural numbers is k(k+1)/2. ` `    ``sum += (k * (k + 1)) / 2; ` `    ``// we can also use code to calculate it ` `    ``// for(int i=1;i<=k;i++) ` `    ``// sum+=i; ` ` `  `    ``// sum of constant term 1 for n times is n ` `    ``sum += k; ` `    ``// we can also use code to calculate it. ` `    ``// for(int i=1;i<=k;i++) ` `    ``// sum+=1; ` `    ``cout << sum << endl; ` `} `

Output :

`107 `

The given code above finds summation of sequence for 3 terms in which,

`Tn = n4+n+1 `

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.