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

Example:

Input: n=10
Output: 220

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

n = int(input("Enter value of n: "))

sum = sum([i*(i+1)/2 for i in range(1, n+1)])
print(sum)

Output:

Enter value of n: 10
220.0

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.

n = int(input("Enter value of n: "))

sum = n*(n+1)*(2*n+4)/12
print(sum)

Output:

Enter value of n: 10
220.0

This Post Has One Comment

  1. Dillu

    Tofind sum of series1^2+(1^2+3^2)+(1^2+3^2+5^2)+….. n terms

Leave a Reply