Python Program To Check Whether The Sequence Is In Arithmetic Progression

A sequence is called to Arithmetic series when the difference between two consecutive numbers remains constant throughout the series. If we express the first term as a and the difference between the terms d, then we can generalize any arithmetic progressive series as [a, a+d, a+2d, a+3d …,a+kd]

In this article, we will create a Python program to check whether the given sequence is in Arithmetic Progression (A.P.) or not.

Python Program To Check Whether A Sequence Is In Arithmetic Progression

def progression(l):

    if len(l) == 1:
        return True
    else:

        diff = l[1] - l[0]
        for index in range(len(l) - 1):
            if not (l[index + 1] - l[index] == diff):
                return False
        return True


print(progression([7, 3, -1, -5]))
print(progression([3, 5, 7, 9, 10]))

Output:

True
False

Explanation

In the above program first, we verifying whether the length of the series is greater than one because there must be at least 2 elements in any Arithmetic series. Next, we are calculating the difference between the first two elements of the sequence then we are examining whether the difference is constant throughout the list by iterating over each element of the list if this evaluates to true the given list is in Arithmetic Progression.

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close