mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-11 05:40:15 +00:00
ae0fc85401
* Fix ruff errors Renamed neural_network/input_data.py to neural_network/input_data.py_tf because it should be left out of the directory for the following reasons: 1. Its sole purpose is to be used by neural_network/gan.py_tf, which is itself left out of the directory because of issues with TensorFlow. 2. It was taken directly from TensorFlow's codebase and is actually already deprecated. If/when neural_network/gan.py_tf is eventually re-added back to the directory, its implementation should be changed to not use neural_network/input_data.py anyway. * updating DIRECTORY.md * Change input_data.py_tf file extension Change input_data.py_tf file extension because algorithms-keeper bot is being picky about it --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
"""
|
|
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
|
|
"""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
def geometric_series(
|
|
nth_term: float,
|
|
start_term_a: float,
|
|
common_ratio_r: float,
|
|
) -> list[float]:
|
|
"""
|
|
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 not all((nth_term, start_term_a, common_ratio_r)):
|
|
return []
|
|
series: list[float] = []
|
|
power = 1
|
|
multiple = common_ratio_r
|
|
for _ in range(int(nth_term)):
|
|
if not series:
|
|
series.append(start_term_a)
|
|
else:
|
|
power += 1
|
|
series.append(float(start_term_a * multiple))
|
|
multiple = pow(float(common_ratio_r), power)
|
|
return series
|
|
|
|
|
|
if __name__ == "__main__":
|
|
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")
|
|
)
|
|
print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n")
|
|
print(geometric_series(nth_term, start_term_a, common_ratio_r))
|