Program to Find the Missing Term of any Arithmetic Progression

Python Program to Find the Missing Term of any Arithmetic Progression

In the previous article, we have discussed Python Program to Find Strong Numbers in a List
Arithmetic progression:

An Arithmetic progression is a mathematical sequence of numbers in which the difference between the consecutive terms is constant.

In general, an arithmetic sequence looks like this:  a, a+d, a+2d, a+3d,…………….

where a = first term

d= common difference

Formula : d= second term – first term

Given an Arithmetic series, and the task is to find the missing term in the given series.

Examples:

Example 1:

Input:

Given Arithmetic Progression series = [10, 14, 18, 22, 30]

Output:

The missing term in the given Arithmetic Progression series is:
26

Example 2:

Input:

Given Arithmetic Progression series = [100, 300, 400, 500, 600, 700]

Output:

The missing term in the given Arithmetic Progression series is:
200

Program to Find the missing term of any Arithmetic progression

Below are the ways to find the missing term in the given Arithmetic progression series.

Method #1: Using For Loop (Static Input)

Approach: 

  • Give the list ( Arithmetic Progression series) as static input and store it in a variable.
  • Calculate the length of the given series using the built-in len() function and store it in another variable.
  • Calculate the common difference by finding the difference between the list’s last and first terms and divide by N and store it in another variable.
  • Take a variable, initialize it with the first term of the given list and store it in another variable say “first term”.
  • Loop from 1 to the length of the given list using for loop.
  • Inside the loop, check if the difference between the list(iterator) and “first term” is not equal to the common difference using if conditional statement.
  • If the statement is true, print the sum of “first term” and common difference
  • Else update the value of variable “first term”  by iterator value.
  • The Exit of the program.

Below is the implementation:

# Give the list ( Arithmetic Progression series) as static input and store it in a variable.
gvn_lst = [100, 200, 400, 500, 600, 700]
# Calculate the length of the given series using the built-in len() function
# and store it in another variable.
lst_len = len(gvn_lst)
# Calculate the common difference by finding the difference between the list's last and
# first terms and divide by N and store it in another variable.
commn_diff = int((gvn_lst[lst_len-1]-gvn_lst[0])/lst_len)
# Take a variable, initialize it with the first term of the given list and store
# it in another variable say "first term".
fst_term = gvn_lst[0]
# Loop from 1 to the length of the given list using for loop.
print("The missing term in the given Arithmetic Progression series is:")
for itr in range(1, lst_len):
 # Inside the loop, check if the difference between the list(iterator) and "first term"
    # is not equal to the common difference using if conditional statement.
    if gvn_lst[itr]-fst_term != commn_diff:
      # If the statement is true, print the sum of "first term" and common difference
        print(fst_term+commn_diff)
        break
    else:
       # Else update the value of variable "first term"  by iterator value.
        fst_term = gvn_lst[itr]

Output:

The missing term in the given Arithmetic Progression series is:
26

Method #2: Using For Loop (User Input)

Approach: 

  • Give the list ( Arithmetic Progression series) as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Calculate the length of the given series using the built-in len() function and store it in another variable.
  • Calculate the common difference by finding the difference between the list’s last and first terms and divide by N and store it in another variable.
  • Take a variable, initialize it with the first term of the given list and store it in another variable say “first term”.
  • Loop from 1 to the length of the given list using for loop.
  • Inside the loop, check if the difference between the list(iterator) and “first term” is not equal to the common difference using if conditional statement.
  • If the statement is true, print the sum of “first term” and common difference
  • Else update the value of variable “first term”  by iterator value.
  • The Exit of the program.

Below is the implementation:

#Give the list ( Arithmetic Progression series) as user input using list(),map(),input(),and split() 
#functions and store it in a variable.
gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given series using the built-in len() function
# and store it in another variable.
lst_len = len(gvn_lst)
# Calculate the common difference by finding the difference between the list's last and
# first terms and divide by N and store it in another variable.
commn_diff = int((gvn_lst[lst_len-1]-gvn_lst[0])/lst_len)
# Take a variable, initialize it with the first term of the given list and store
# it in another variable say "first term".
fst_term = gvn_lst[0]
# Loop from 1 to the length of the given list using for loop.
print("The missing term in the given Arithmetic Progression series is:")
for itr in range(1, lst_len):
 # Inside the loop, check if the difference between the list(iterator) and "first term"
    # is not equal to the common difference using if conditional statement.
    if gvn_lst[itr]-fst_term != commn_diff:
      # If the statement is true, print the sum of "first term" and common difference
        print(fst_term+commn_diff)
        break
    else:
       # Else update the value of variable "first term"  by iterator value.
        fst_term = gvn_lst[itr]

Output:

Enter some random List Elements separated by spaces = 100 300 400 500 600 700
The missing term in the given Arithmetic Progression series is:
200

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.