Sum of geometric progression series in Python

In this tutorial, we will learn to display the sum of geometric progression series in Python. In a geometric progression, the ratio of any two consecutive numbers is the same. And this ratio is known as the common ratio of the GP series. Here, we will learn to find the successive elements in the series, the total sum of ‘n’ terms of the geometric progression series. Also, we will implement the same using a Python program. If you want the Python program to calculate the sum of ‘n’ terms of a GP series, you are at the right place.

Geometric progression series

In mathematics, a geometric progression series is a series in which the ratio of any two consecutive terms is the same. So, we can find the successive term by multiplying the common ratio with the previous term. Let us take an example of a geometric series-

Consider the first term and common ratio as 1 and 2 respectively.
So, the GP series is- 1, 2, 4, 8, 16, 32, 64, ….. upto ‘n’ terms.

To calculate the successive term, we use the formula –
[nth term] = [(n-1)th term] * common_ratio

Python program to calculate the sum of ‘n’ terms of a geometric progression series

Now, we will see a Python program to calculate the sum of ‘n’ terms of a geometric progression series. So, we will have to find the ‘n’ terms of the series and add each term to find the required sum. Firstly, we will take the total number of terms, the first term of the GP series, and the common ratio. Finally, we will display individual terms and then the sum of the series as output. The Python program is given below-

n = int(input("ENTER TOTAL NUMBERS IN GP SERIES : "))
a = int(input("ENTER FIRST NUMBER OF GP SERIES : "))
r = int(input("ENTER THE COMMON RATIO : "))
print("THE GEOMETRIC SERIES IS - ")
print(a)
prev_term = a
sum_gp = a
for i in range(n):
  ith_term = r * prev_term
  print("{}".format(ith_term))
  prev_term = ith_term
  sum_gp = sum_gp + ith_term
print("\nThe Sum of Geometric Progression Series = " , sum_gp)

Python program output

The above Python program displays the geometric series and the sum of ‘n’ terms of the series. So, the output after sample execution of the program is as follows-

[email protected]:~/python$ python3 gp.py
ENTER TOTAL NUMBERS IN GP SERIES : 10
ENTER FIRST NUMBER OF GP SERIES : 2
ENTER THE COMMON RATIO : 2
THE GEOMETRIC SERIES IS - 
2
4
8
16
32
64
128
256
512
1024
2048

The Sum of Geometric Progression Series =  4094
[email protected]:~/python$

So, the sum of geometric progression series with first term 2, common ratio 2, and the number of terms 10 is 4094.

Thanks for reading this tutorial. I hope it helps you.

Also read: Finding the Sum of Cosine Series in Python

Leave a Reply

Your email address will not be published. Required fields are marked *