Sequence and Series in Python
Last Updated :
30 May, 2024
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:
- an is the n-th term,
- a1 is the first term,
- d is the common difference,
- n is the term number.
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)
OutputArithmetic 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:
- anā is the n-th term,
- a1ā is the first term,
- r is the common ratio,
- n is the term number.
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)
OutputGeometric 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)
OutputSum 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:
- an is the n-th term,
- a1 is the first term,
- d is the common difference,
- n is the term number.
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)
OutputHarmonic 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.
Similar Reads
Sequences and Series
A sequence is an ordered list of numbers following a specific rule. Each number in a sequence is called a "term." The order in which terms are arranged is crucial, as each term has a specific position, often denoted as anâ, where n indicates the position in the sequence.For example:2, 5, 8, 11, 14,
7 min read
Subarray, Subsequence and Subsets in Python
Algorithms and data manipulation are areas where it is important to grasp the ideas behind subarrays, subsequences as well as subsets. This article introduces the concepts of subarrays, subsequences along with subsets in Python. You will find out what these terms actually mean and how they differ fr
7 min read
String Subsequence and Substring in Python
Subsequence and Substring both are parts of the given String with some differences between them. Both of them are made using the characters in the given String only. The difference between them is that the Substring is the contiguous part of the string and the Subsequence is the non-contiguous part
5 min read
Sequences and Series Formulas
Sequences and Series Formulas: In mathematics, sequence and series are the fundamental concepts of arithmetic. A sequence is also referred to as a progression, which is defined as a successive arrangement of numbers in an order according to some specific rules. A series is formed by adding the eleme
10 min read
Sequential Data Analysis in Python
Sequential data, often referred to as ordered data, consists of observations arranged in a specific order. This type of data is not necessarily time-based; it can represent sequences such as text, DNA strands, or user actions.In this article, we are going to explore, sequential data analysis, it's t
8 min read
How to find a term in a sequence?
Arithmetic is the part of mathematics that consists of the Study of numbers and operations performed on that numbers. such operations are addition, subtraction, multiplication, division, exponentiation, and extraction of roots. The sequence is a collection/enumerated collection of objects/numbers wh
6 min read
Recurrence Relation in python
Recurrence relation is an equation that recursively defines a sequence, where the next term is a function of the previous terms. Recurrence relations are commonly used to describe the runtime of recursive algorithms in computer science and to define sequences in mathematics. What is a Recurrence Rel
3 min read
Sequences in Maths
In mathematics, a sequence is an ordered list of numbers or objects that follows a specific rule or pattern. Each number in the sequence is called a term, and the position of a term in the sequence is determined by its index.Types of Sequences1. Finite Sequence: A sequence that has a limited number
3 min read
Mathematical Algorithms - Sequence & Series
Mathematical algorithms are step-by-step procedures used to solve math problems. This article looks at sequences and series, which are important parts of these algorithms. Sequences are ordered sets of numbers, while series are the sums of these numbers. Understanding sequences and series is vital f
4 min read
Sequence and Series Word Problems | Class 11 Maths
Sequences and series have several important applications in several spheres of human activities. When sequences follow some specific patterns, they are usually called progressions. Arithmetic and Geometric progressions are some examples of commonly occurring progressions. Let's see some problems on
9 min read