Write a Python Program to Find Sum of Series [1+(1+2)+(1+2+3)+–+(1+2+3+–+n)].

Example:

We can solve this series using two approaches one by using a loop and another by using direct formula. Let’s see both the methods.

Method 1- Using Loop and List Comprehension

Each nth term of the series is the sum of the n terms i.e n*(n+1)/2. So we need to loop from 1 to n (i) and find the sum of i*(i+1)/2.

Recommended: Python List Comprehension

Output

Method 2- Using Formula

nth = n*(n + 1)/2 = (n^2 + n)/2

Sum of n-terms of series
Σnth a = Σ(n^2 + n)/2

= 1/2 Σ [ n^2 ] + Σ [ n ]

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

= n(n+1)(2n+4)/12

So using the following formula n(n+1)(2n+4)/12 we can find the sum of the same series in python.

Output

If you have any suggestions or doubts then comment below.

Leave a Reply