11
11
12
12
13
13
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 ]:
16
18
"""
17
19
Pure Python implementation of Geometric Series algorithm
18
20
@@ -21,28 +23,27 @@ def geometric_series(
21
23
:param common_ratio_r : The common ratio between all the terms
22
24
:return: The Geometric Series starting from first term a and multiple of common
23
25
ration with first term with increase in power till last term (nth term)
24
-
25
26
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]
28
29
>>> geometric_series(4.0, 2.0, 2.0)
29
30
[2.0, 4.0, 8.0, 16.0]
30
31
>>> geometric_series(4.1, 2.1, 2.1)
31
32
[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)
37
38
[]
38
- >>> geometric_series(0, 100.0 , 500.0 )
39
+ >>> geometric_series(0, 100, 500)
39
40
[]
40
- >>> geometric_series(1.0 , 1.0 , 1.0 )
41
- [1.0 ]
41
+ >>> geometric_series(1, 1, 1)
42
+ [1]
42
43
>>> geometric_series(0, 0, 0)
43
44
[]
44
45
"""
45
- if 0 in ( nth_term , start_term_a , common_ratio_r ):
46
+ if not all (( nth_term , start_term_a , common_ratio_r ) ):
46
47
return []
47
48
series : list [float ] = []
48
49
power : int = 1
@@ -59,5 +60,14 @@ def geometric_series(
59
60
60
61
if __name__ == "__main__" :
61
62
import doctest
62
-
63
63
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