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:
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:
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
Sequence and Series in Python
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.