In the previous article we have explored the use of various membership operators in Python. Now we will look at the implementation of the Arithmetic Progression in Python. An arithmetic progression is a numerical sequence a 1 , a 2 , …, a n , … in which, for each natural n, the equality is satisfied. a n + 1 = a n + d, where d is the difference of the arithmetic progression. This formula can be described in words as follows: each member of the arithmetic progression is equal to the previous one, added with the same number d.
The difference between the next and the previous terms, that is, the difference in arithmetic progression, can be found by the formula: d = a(n) – a(n – 1)
Implementation of Arithmetic Progression in Python
Let us look at an example to solve to improve our understanding of arithmetic progression in Python:
A person gets 5000 salary on 1st August 2020. The salary increases by 2000 every month. Find the salary that the person is going to get on 1st August 2025
To solve this program first we must utilize the formula:
nth Term = a + (n-1) d
Where a-> First Term
d-> Common Difference
Since the question itself provides to us the value of the common difference, that is of the increment by 2000 every month. We need to find out the number of months from 1st August 2020 until 1st August 2025. The time period is of 5 years which can be converted to about 60 months, since we also include the ending month we get a value of about 61 months.
Now by utilizing the formula itself, we can derive:
nth Term = a + (n-1) d = 5000 + (61-1) x 2000 = 5000 + 60 x 2000 = 5000 + 120000 = 125000
Implementation of formulaic logic as Arithmetic Progression in Python
First we need to accept user-based input for the various values required to calculate the end result. Specifically the values of a, d and n for the formula. Where a represents the first term, d represents the common difference and n represents the number
# A Python program to handle Arithmetic Progression of Terms based on User Input a=int(input(" Enter first term (a):")) d=int(input("Enter common difference (d):")) n=int(input("Enter number(n):")) res = a + (n-1) * d print(res)
Let us look at example problems involving Arithmetic Progression
1. Given three integer, a,d and n. Where the first term is 5, 2 is the common difference of an A.P. Calculate the 5th term of A.P. The nth term is given by an = a + (n-1)d. Input: a = 5 d = 2 n = 5
Output: Enter first term (a):5 Enter common difference (d):2 Enter number(n):5 13
After specifying the values and running the program, the value of the variable a as 5, d as 2 and n as 5. We obtain the result 13 as the resultant output from the program since anth = a + (n-1)d = 5 + (5-1)*2 = 5 + 8 = 13
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
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: anth = a + (n-1)d = 10 + (101-1)*10= 10 + 1000 = 1010.
Thus we have explored the concept of the implementation of Arithmetic Progression in Python through various examples, in the next article will take a look at Geometric Progression and it’s implementation in Python.