Geometric Progression in Python Code

In the previous article we have seen the implementation of Arithmetic Progression in Python, in this article we will similarly explore the Geometric Progression in Python, the concepts and the logic or the formulae we operate by. A geometric progression is a sequence of nonzero numbers, each term of which, starting from the second, is equal to the previous term multiplied by the same number. This number is called the common ratio of the geometric progression. A geometric progression is called increasing when the absolute value of its common ratio is greater than one, and decreasing when it is less than one.

Implementation of Geometric Progression in Python

Let us look at an example to solve to improve our understanding of Geometric progression in Python:

A person gets 5000 salary on 1st January 2020. The salary doubles every year. Find the salary that the person is going to get on 1st January 2030

To solve this problem we must utilize the forumla:

n-th term of Geometric Progression = arn-1

Where a-> First Term

r-> Common ratio

The starting term or first term is of the value 5000. We need to find out the number of years from 1st January 2020 until 1st January 2030. The time period is about 11 years

Now by utilizing the formula itself we can derive:

n-th term of Geometric Progression = arn-1
                                   = 5000 x 211-1
                                   = 5000 x 1024
                                   = 5120000

Implementation of formulaic logic as Geometric Progression in Python

We write the program to accept various user given values as input for the formula, the program should accept the values of a,n and r from the user

#A Python program to handle Geometric Progression of Terms based on User Input

#Collect input from the user for first term
a=int(input(" Enter first term (a):"))
#Collect input from the user for the value of 'r'
r=int(input("Enter the value of r (r):"))
#Collect input from the user for the value of duration number
n=int(input("Enter number(n):"))

#Implementation of Geometric Progression Formula into Python
res= a * r ** (n-1)

#Statement to print out the final end result of calculation
print(res)

Let us look at example problems involving Geometric Progression :

Given three integer, a, r and n. Where a is the first term, r is the common ratio of a G.P.  Calculate the nth term of GP.  The nth term is given by an= a*rn-1
Input:
a = 2
r = 2
n = 10
Output:
Enter first term (a):2
Enter the value of r (r):2
Enter number(n):10
1024

After specifying the values and running the program, the value of the variable a as 2, r as 2 and n as 10. We obtain the result 1024 as the resultant output from the program since an = arn-1 = 2*210-1 = 1024

2. Given three integer, a,d and n. Where the first term is 10 , 10 is the common difference of an A.P.  Calculate the 101th term of A.P.  The nth term is given by an = a + (n-1)d.
Input:
a = 10
d = 10
n = 101
Output:
Enter first term (a):1
Enter the value of r (r):10
Enter number(n):5
10000

After specifying the values and running the program, the value of the variable a as 10, d as 10 and n as 101. We obtain the result 1010 as the resultant output from the program since â€<an = a*rn-1 = 1* 105-1 =10000

We have covered the concept of Geometric Progression and the implementation of Geometric Progression in Python, in the next article we will some programs such as finding the sum of natural numbers, finding the last digit in and day before n days.

Leave a Comment