Skip to content

Commit dd1ff0a

Browse files
Update geometric_series.py
1 parent d112ce4 commit dd1ff0a

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

maths/series/geometric_series.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212

1313
def geometric_series(
14-
nth_term: float, start_term_a: float, common_ratio_r: float
15-
) -> list[float]:
14+
nth_term: float | int,
15+
start_term_a: float | int,
16+
common_ratio_r: float | int,
17+
) -> list[float | int]:
1618
"""
1719
Pure Python implementation of Geometric Series algorithm
1820
@@ -21,28 +23,27 @@ def geometric_series(
2123
:param common_ratio_r : The common ratio between all the terms
2224
:return: The Geometric Series starting from first term a and multiple of common
2325
ration with first term with increase in power till last term (nth term)
24-
2526
Examples:
26-
>>> geometric_series(4.0, 2.0, 2.0)
27-
[2.0, 4.0, 8.0, 16.0]
27+
>>> geometric_series(4, 2, 2)
28+
[2, 4.0, 8.0, 16.0]
2829
>>> geometric_series(4.0, 2.0, 2.0)
2930
[2.0, 4.0, 8.0, 16.0]
3031
>>> geometric_series(4.1, 2.1, 2.1)
3132
[2.1, 4.41, 9.261000000000001, 19.448100000000004]
32-
>>> geometric_series(4.0, 2.0, -2.0)
33-
[2.0, -4.0, 8.0, -16.0]
34-
>>> geometric_series(4.0, -2.0, 2.0)
35-
[-2.0, -4.0, -8.0, -16.0]
36-
>>> geometric_series(-4.0, 2.0, 2.0)
33+
>>> geometric_series(4, 2, -2)
34+
[2, -4.0, 8.0, -16.0]
35+
>>> geometric_series(4, -2, 2)
36+
[-2, -4.0, -8.0, -16.0]
37+
>>> geometric_series(-4, 2, 2)
3738
[]
38-
>>> geometric_series(0, 100.0, 500.0)
39+
>>> geometric_series(0, 100, 500)
3940
[]
40-
>>> geometric_series(1.0, 1.0, 1.0)
41-
[1.0]
41+
>>> geometric_series(1, 1, 1)
42+
[1]
4243
>>> geometric_series(0, 0, 0)
4344
[]
4445
"""
45-
if 0 in (nth_term, start_term_a, common_ratio_r):
46+
if not all((nth_term, start_term_a, common_ratio_r)):
4647
return []
4748
series: list[float] = []
4849
power: int = 1
@@ -59,5 +60,14 @@ def geometric_series(
5960

6061
if __name__ == "__main__":
6162
import doctest
62-
6363
doctest.testmod()
64+
65+
nth_term = float(input("Enter the last number (n term) of the Geometric Series"))
66+
start_term_a = float(input("Enter the starting term (a) of the Geometric Series"))
67+
common_ratio_r = float(
68+
input(
69+
"Enter the common ratio between two terms (r) of the Geometric Series"
70+
)
71+
)
72+
print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n")
73+
print(geometric_series(nth_term, start_term_a, common_ratio_r))

0 commit comments

Comments
 (0)