Open In App

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 

adding these all terms, 
 

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 <bits/stdc++.h>
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;
}




k = 3
sum = 0
 
# finding sum of n^4 terms
for i in range(1, k+1):
    sum += (i * i * i * i)
     
# sum of first k natural numbers is k(k+1)/2.
sum += (k * (k + 1)) / 2
 
# sum of constant term 1 for n times is n
sum += k
 
print(int(sum))
 
# This code is contributed by ninja_hattori.

Output : 
 

107 

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

Tn = n4+n+1 

 


Article Tags :