Sequence and Series in Python (original) (raw)

Last Updated : 23 Jul, 2025

Sequences and series are fundamental concepts in mathematics. A Sequence is an ordered list of numbers following a specific pattern, while a series is the sum of the elements of a sequence. This tutorial will cover arithmetic sequences, geometric sequences, and how to work with them in Python.

Arithmetic Sequence:

An arithmetic sequence is a sequence of numbers in which the difference between consecutive terms is constant. This difference is called the common difference (d).

**Formula: an​ = a1​ + (n−1) * d

where:

**Sum of the first n terms: Sn​ = (n/2)​ * (2 * a1​ + (n−1) * d)

Python Implementation:

Python `

def arithmetic_sequence(a1, d, n): return [a1 + (i * d) for i in range(n)]

def arithmetic_sum(a1, d, n): return (n / 2) * (2 * a1 + (n - 1) * d)

Example usage

a1 = 2 d = 3 n = 10 sequence = arithmetic_sequence(a1, d, n) sequence_sum = arithmetic_sum(a1, d, n) print("Arithmetic Sequence:", sequence) print("Sum of Arithmetic Sequence:", sequence_sum)

`

Output

Arithmetic Sequence: [2, 5, 8, 11, 14, 17, 20, 23, 26, 29] Sum of Arithmetic Sequence: 155.0

Geometric Sequence:

A geometric sequence is a sequence of numbers where each term after the first is found by multiplying the previous term by a constant called the common ratio (r).

1. Working with Infinite Series

**Formula: an​ = a1​ * r^(n−1)

Where:

**Sum of the first n terms (if r != 1): Sn​ = (a1/(​1−r^n))/(1−r)​

Python Implementation:

Python `

def geometric_sequence(a1, r, n): return [a1 * (r ** i) for i in range(n)]

def geometric_sum(a1, r, n): if r == 1: return a1 * n return a1 * (1 - r ** n) / (1 - r)

Example usage

a1 = 2 r = 3 n = 5 sequence = geometric_sequence(a1, r, n) sequence_sum = geometric_sum(a1, r, n) print("Geometric Sequence:", sequence) print("Sum of Geometric Sequence:", sequence_sum)

`

Output

Geometric Sequence: [2, 6, 18, 54, 162] Sum of Geometric Sequence: 242.0

2. Working with Infinite Series

While working with infinite series, it's essential to know their convergence properties. For instance, an infinite geometric series converges if the absolute value of the common ratio is less than 1.

**Sum of an infinite geometric series (|r| < 1): S = a1/(1−r)​​

Python Implementation:

Python `

def infinite_geometric_sum(a1, r): if abs(r) >= 1: return float('inf') # or raise an exception return a1 / (1 - r)

Example usage

a1 = 2 r = 0.5 infinite_sum = infinite_geometric_sum(a1, r) print("Sum of Infinite Geometric Series:", infinite_sum)

`

Output

Sum of Infinite Geometric Series: 4.0

Harmonic Sequence:

A harmonic series is a series of numbers whose reciprocals form an arithmetic sequence. The n-th term of a harmonic sequence can be given by the reciprocal of the n-th term of an arithmetic sequence.

**Formula: an​ = 1 / (a1​ + (n−1) * d)

where:

**Python Implementation:

Python `

Python3 code to generate Harmonic Progression

and calculate the sum of the progression.

import math

Function that generates the harmonic

progression and calculates the sum of

its elements by iterating.

n = 5 AP = [0] * (n + 5)

def generateAP(a, d, n): sum = 0 for i in range(1, n + 1):

    # HP = 1/AP
    # In AP, ith term is calculated
    # by a+(i-1)d;
    AP[i] = (a + (i - 1) * d)

    # Calculating the sum.
    sum += float(1) / float((a + (i - 1) * d))
return sum

Function that uses riemann sum method to calculate

the approximate sum of HP in O(1) time complexity

def sumApproximation(a, d, n):

return math.log((2 * a + (2 * n - 1) * d) /
                (2 * a - d)) / d

Driver Code

a = 12 d = 12 #n = 5;

Generating AP from the above data

sum = generateAP(a, d, n)

Generating HP from the generated AP

print("Harmonic Progression :") for i in range(1, n + 1): print("1 /", AP[i], end=" ") print("") str1 = "" str1 = str1 + str(sum) str1 = str1[0:4]

print("Sum of the generated harmonic", "progression :", str1) sum = sumApproximation(a, d, n)

str1 = "" str1 = str1 + str(sum) str1 = str1[0:4] print("Sum of the generated harmonic", "progression using approximation :", str1)

`

Output

Harmonic Progression : 1 / 12 1 / 24 1 / 36 1 / 48 1 / 60 Sum of the generated harmonic progression : 0.19 Sum of the generated harmonic progression using approximation : 0.19

Sequences and series are fundamental concepts in mathematics, and they have many applications in computer science, especially in algorithm design and numerical computations. By mastering these concepts, we can efficiently solve a wide range of problems in mathematics, data analysis, and algorithm development.