2019-12-14 05:46:02 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2021-11-07 15:13:58 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
def geometric_series(
|
2023-08-09 07:55:30 +00:00
|
|
|
nth_term: float,
|
|
|
|
start_term_a: float,
|
|
|
|
common_ratio_r: float,
|
|
|
|
) -> list[float]:
|
2021-11-07 15:13:58 +00:00
|
|
|
"""
|
|
|
|
Pure Python implementation of Geometric Series algorithm
|
|
|
|
|
2019-12-14 05:46:02 +00:00
|
|
|
: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)
|
2021-11-07 15:13:58 +00:00
|
|
|
[2, 4.0, 8.0, 16.0]
|
2019-12-14 05:46:02 +00:00
|
|
|
>>> geometric_series(4.0, 2.0, 2.0)
|
2021-11-07 15:13:58 +00:00
|
|
|
[2.0, 4.0, 8.0, 16.0]
|
2019-12-14 05:46:02 +00:00
|
|
|
>>> geometric_series(4.1, 2.1, 2.1)
|
2021-11-07 15:13:58 +00:00
|
|
|
[2.1, 4.41, 9.261000000000001, 19.448100000000004]
|
2019-12-14 05:46:02 +00:00
|
|
|
>>> geometric_series(4, 2, -2)
|
2021-11-07 15:13:58 +00:00
|
|
|
[2, -4.0, 8.0, -16.0]
|
2019-12-14 05:46:02 +00:00
|
|
|
>>> geometric_series(4, -2, 2)
|
2021-11-07 15:13:58 +00:00
|
|
|
[-2, -4.0, -8.0, -16.0]
|
2019-12-14 05:46:02 +00:00
|
|
|
>>> geometric_series(-4, 2, 2)
|
|
|
|
[]
|
|
|
|
>>> geometric_series(0, 100, 500)
|
|
|
|
[]
|
|
|
|
>>> geometric_series(1, 1, 1)
|
|
|
|
[1]
|
|
|
|
>>> geometric_series(0, 0, 0)
|
|
|
|
[]
|
|
|
|
"""
|
2021-11-07 15:13:58 +00:00
|
|
|
if not all((nth_term, start_term_a, common_ratio_r)):
|
|
|
|
return []
|
2023-08-09 07:55:30 +00:00
|
|
|
series: list[float] = []
|
2019-12-14 05:46:02 +00:00
|
|
|
power = 1
|
|
|
|
multiple = common_ratio_r
|
|
|
|
for _ in range(int(nth_term)):
|
2022-11-20 11:00:27 +00:00
|
|
|
if not series:
|
2019-12-14 05:46:02 +00:00
|
|
|
series.append(start_term_a)
|
|
|
|
else:
|
|
|
|
power += 1
|
2021-11-07 15:13:58 +00:00
|
|
|
series.append(float(start_term_a * multiple))
|
2019-12-14 05:46:02 +00:00
|
|
|
multiple = pow(float(common_ratio_r), power)
|
|
|
|
return series
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-11-07 15:13:58 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|
|
|
|
|
|
|
|
nth_term = float(input("Enter the last number (n term) of the Geometric Series"))
|
|
|
|
start_term_a = float(input("Enter the starting term (a) of the Geometric Series"))
|
|
|
|
common_ratio_r = float(
|
|
|
|
input("Enter the common ratio between two terms (r) of the Geometric Series")
|
2019-12-14 05:46:02 +00:00
|
|
|
)
|
|
|
|
print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n")
|
|
|
|
print(geometric_series(nth_term, start_term_a, common_ratio_r))
|