Program to Find Sum of Geometric Progression Series

Python Program to Find Sum of Geometric Progression Series

In the previous article, we have discussed Python Program to Repeat String N times with Separator
Geometric progression series

A geometric progression series is one in which any two consecutive terms have the same ratio. As a result, we can find the subsequent term by multiplying the common ratio by the previous term.

This is how the series looks:   a, ar, ar2, ar3, ar4, . . . . .

where common ratio(r)=2nd term/1st term (T2/T1) or (T3/T2)

Standard Formula to find the sum of series in G.P =  a(1 – rn)/(1 – r)

Given First term(a), common ratio(r), Nth term(total number of terms ) in Series, The task is to find Sum of the Geometric Progression Series.

Example: 2,6,18,54,162,486,1458,. . . . . . . .

Here a=2 , r=6/2 =3 , let n=10

Formula to find Nth term = arn1  

= 39366

sum of series = a(1 – rn)/(1 – r) =59048

Examples:

Example1:

Input:

Given Total number of terms=4
Given First Term = 3
Given common ratio = 3

Output:

The sum of the given geometric progression series = 120

Example 2:

Input:

Given Total number of terms=6
Given First Term = 4 
Given common ratio = 5

Output:

The sum of the given geometric progression series = 15624

Program to Find Sum of Geometric Progression Series

Below are the ways to find the Sum of the Geometric Progression Series.

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the Total number of terms as static input and store it in a variable.
  • Give the first term as static input and store it in another variable.
  • Give the Common Ratio as static input and store it in another variable.
  • Calculate the given Sum of Geometric Progression Series by using the standard mathematical formula a(1 – rn)/(1 – r) and store it in a variable.
  • Print the sum of the Geometric Progression series.
  • The Exit of the program.

Below is the implementation:

# Import math module using import keyword.
import math
# Give the Total number of terms as static input and store it in a variable.
tot_trms = 10
# Give the first term as static input and store it in a variable.
fst_trm = 2
# Give the Common Ratio as static input and store it in a variable.
commn_diff = 3
# Calculate the given Sum of Geometric Progression Series by using standard mathematical formula
# a(1 – r**n)/(1 – r) and store it in a variable.
sum_geoprog = (fst_trm*(1-(commn_diff)**tot_trms))//(1-commn_diff)
# Print the sum of Geometric Progression series .
print("The sum of the given geometric progression series = ", sum_geoprog)

 

Output:

The sum of the given geometric progression series =  59048

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the Total number of terms as User input using the int(input()) function and store it in a variable.
  • Give the first term as User input using the int(input()) function and store it in another variable.
  • Give the Common Ratio as User input using the int(input()) function and store it in another variable.
  • Calculate the Sum of Geometric Progression Series by using the standard mathematical formula a(1 – rn)/(1 – r) and store it in a variable.
  • Print the sum of the Geometric Progression series.
  • The Exit of the program.

Below is the implementation:

#Import math module using the import keyword.
import math
#Give the Total number of terms as User input using the int(input()) function and store it in a variable.
tot_trms = int(input("Given Total number of terms ="))
#Give the first term as User input using the int(input()) function and store it in another variable.
fst_trm = int(input("Given First Term = "))
#Give the Common Ratio as User input using the int(input()) function  and store it in another variable.
commn_diff = int(input("Given common ratio = "))
#Calculate the Sum of Geometric Progression Series by using standard mathematical formula 
#a(1 – r**n)/(1 – r) and store it in a variable.
sum_geoprog = (fst_trm*(1-(commn_diff)**tot_trms))//(1-commn_diff)
#Print the sum of Geometric Progression series .
print("The sum of the given geometric progression series = ", sum_geoprog)

Output:

Given Total number of terms =6
Given First Term = 4
Given common ratio = 5
The sum of the given geometric progression series = 15624

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.