Arithmetic or geometric sequence in python

When working with sequences in Python, it is important to understand the difference between arithmetic and geometric sequences. An arithmetic sequence is a sequence of numbers in which the difference between consecutive terms is constant. On the other hand, a geometric sequence is a sequence of numbers in which each term after the first is found by multiplying the previous term by a fixed, non-zero number called the common ratio.

Option 1: Using a for loop

One way to solve this problem is by using a for loop. We can iterate through the given sequence and check if the difference between consecutive terms is constant or if the ratio between consecutive terms is constant.


def is_arithmetic(sequence):
    difference = sequence[1] - sequence[0]
    for i in range(2, len(sequence)):
        if sequence[i] - sequence[i-1] != difference:
            return False
    return True

def is_geometric(sequence):
    ratio = sequence[1] / sequence[0]
    for i in range(2, len(sequence)):
        if sequence[i] / sequence[i-1] != ratio:
            return False
    return True

sequence = [1, 3, 5, 7, 9]
if is_arithmetic(sequence):
    print("The sequence is arithmetic.")
elif is_geometric(sequence):
    print("The sequence is geometric.")
else:
    print("The sequence is neither arithmetic nor geometric.")

Option 2: Using list comprehension

Another approach is to use list comprehension to check if the difference between consecutive terms or the ratio between consecutive terms is constant.


def is_arithmetic(sequence):
    difference = sequence[1] - sequence[0]
    return all(sequence[i] - sequence[i-1] == difference for i in range(2, len(sequence)))

def is_geometric(sequence):
    ratio = sequence[1] / sequence[0]
    return all(sequence[i] / sequence[i-1] == ratio for i in range(2, len(sequence)))

sequence = [1, 3, 5, 7, 9]
if is_arithmetic(sequence):
    print("The sequence is arithmetic.")
elif is_geometric(sequence):
    print("The sequence is geometric.")
else:
    print("The sequence is neither arithmetic nor geometric.")

Option 3: Using numpy

If you have numpy installed, you can also use the numpy library to solve this problem. Numpy provides functions to check if a sequence is arithmetic or geometric.


import numpy as np

def is_arithmetic(sequence):
    return np.all(np.diff(sequence) == sequence[1] - sequence[0])

def is_geometric(sequence):
    return np.all(np.diff(sequence) == sequence[1] / sequence[0])

sequence = [1, 3, 5, 7, 9]
if is_arithmetic(sequence):
    print("The sequence is arithmetic.")
elif is_geometric(sequence):
    print("The sequence is geometric.")
else:
    print("The sequence is neither arithmetic nor geometric.")

Among the three options, using numpy is the most efficient and concise solution. Numpy’s functions allow us to check if a sequence is arithmetic or geometric in a single line of code. Additionally, numpy is a widely used library in scientific computing and provides many other useful functions for numerical operations.

Rate this post

6 Responses

    1. I couldnt disagree more! While list comprehension may be concise, it can also be cryptic and difficult to understand for beginners. Its important to prioritize readability and maintainability over brevity. Ill stick to a more explicit approach, thank you very much.

    1. Magic spells are not real. We live in a world where practical solutions are necessary. So lets stick to reality and discuss viable options that can actually solve the problem at hand.

Leave a Reply

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

Table of Contents