geometric series Algorithm

The geometric series algorithm is a mathematical technique used to determine the sum of a geometric progression, which is a sequence of numbers in which each term is found by multiplying the previous term by a constant. This constant is known as the common ratio (r), and the series is denoted by the general formula ar^n, where a is the initial term, r is the common ratio, and n is the number of terms. The geometric series algorithm provides an efficient way to calculate the sum of this type of sequence, which has wide-ranging applications in various fields such as physics, computer science, and finance. To compute the sum of a geometric series, the algorithm utilizes the formula S = a(1 - r^n) / (1 - r), where S is the sum, a is the initial term, r is the common ratio, and n is the number of terms. This formula is derived from the observation that the sum of a geometric series can be represented by the difference between two successive terms of another geometric series with the same common ratio. By applying this formula, the algorithm can quickly and accurately calculate the sum of a given geometric progression, even for large values of n. This makes the geometric series algorithm a powerful tool in solving problems that involve repetitive patterns, exponential growth or decay, and various other mathematical phenomena that can be modeled by geometric sequences.
"""
This is a pure Python implementation of the Geometric Series algorithm
https://en.wikipedia.org/wiki/Geometric_series

Run the doctests with the following command:
python3 -m doctest -v geometric_series.py
or
python -m doctest -v geometric_series.py
For manual testing run:
python3 geometric_series.py
"""


def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> list:
    """Pure Python implementation of Geometric Series algorithm
    :param nth_term: The last term (nth term of Geometric Series)
    :param start_term_a : The first term of Geometric Series
    :param common_ratio_r : The common ratio between all the terms
    :return: The Geometric Series starting from first term a and multiple of common
        ration with first term with increase in power till last term (nth term)
    Examples:
    >>> geometric_series(4, 2, 2)
    [2, '4.0', '8.0', '16.0']
    >>> geometric_series(4.0, 2.0, 2.0)
    [2.0, '4.0', '8.0', '16.0']
    >>> geometric_series(4.1, 2.1, 2.1)
    [2.1, '4.41', '9.261000000000001', '19.448100000000004']
    >>> geometric_series(4, 2, -2)
    [2, '-4.0', '8.0', '-16.0']
    >>> geometric_series(4, -2, 2)
    [-2, '-4.0', '-8.0', '-16.0']
    >>> geometric_series(-4, 2, 2)
    []
    >>> geometric_series(0, 100, 500)
    []
    >>> geometric_series(1, 1, 1)
    [1]
    >>> geometric_series(0, 0, 0)
    []
    """
    if "" in (nth_term, start_term_a, common_ratio_r):
        return ""
    series = []
    power = 1
    multiple = common_ratio_r
    for _ in range(int(nth_term)):
        if series == []:
            series.append(start_term_a)
        else:
            power += 1
            series.append(str(float(start_term_a) * float(multiple)))
            multiple = pow(float(common_ratio_r), power)
    return series


if __name__ == "__main__":
    nth_term = input("Enter the last number (n term) of the Geometric Series")
    start_term_a = input("Enter the starting term (a) of the Geometric Series")
    common_ratio_r = input(
        "Enter the common ratio between two terms (r) of the Geometric Series"
    )
    print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n")
    print(geometric_series(nth_term, start_term_a, common_ratio_r))

LANGUAGE:

DARK MODE: