Python program to find the missing term of any Arithmetic progression
In this tutorial, We will see the Python program to find the missing term of any Arithmetic progression. In this type of problem, An array given by the user that represent the elements of the Arithmetic progression in order and one element is missing from the given progression that we have to find through the Python program. To solve this we have to learn something about Arithmetic progression.
What is Arithmetic progression?
In mathematic, An Arithmetic progression is a sequence of numbers such that the difference between the consecutive term is constant.
Algorithm to solve this problem:-
- First of all, we find the length of the given array and stored in variable N.
- To find the common difference of the Arithmetic progression, we will find the difference between the last term and the initial term of the array and divided by N.
- We will assign a variable(s) as the initial term of the given array.
- To check which term is missing, we will run a for loop of range from 1 to N and check for every loop if the difference of loop term and s is not equal to common difference then the missing number is the sum of s and common difference else update the value of variable s by loop element.
Find the missing term of any Arithmetic progression in Python
Here, By using the above algorithm, we will write the Python program. Let us assume the Arithmetic progression given by the user is
a=[2,6,14,18] N=len(a) c=int((a[N-1]-a)/N) s=a for k in range(1,N): if a[k]-s!=c: print(s+c) break else: s=a[k]
So Guy’s, I hope you find it useful.
You may also read: