-
Notifications
You must be signed in to change notification settings - Fork 4
/
series.py
78 lines (61 loc) · 2.11 KB
/
series.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Functions on Arithmetic and Geometric Progression Series.
"""
import math
def nth_term_ap(a: float, d: float, n: int) -> float:
'''
Returns the nth term of an Arithmetic Progression.
Parameters:
a: First term of Progression
d: Common difference of the Progression
n: nth term to be calculated
Returns:
The value of the nth term
'''
if n<0:
raise ValueError("n cannot be negative")
return (a + (n - 1) * d)
def nth_term_gp(a: float, r: float, n: int) -> float:
'''
Returns the nth term of a Geometric Progression.
Parameters:
a: First term of Progression
r: Common ratio of the Progression
n: nth term to be calculated
Returns:
The value of the nth term
'''
if n<0:
raise ValueError("n cannot be negative")
return ( a * (int)(math.pow(r, n - 1)) )
def sum_ap(a: float, d: float, n: int) -> float:
'''
Returns the sum upto n terms of an Arithmetic Progression.
Parameters:
a: First term of Progression
d: Common difference of the Progression
n: Value (int) upto which sum is to be calculated
Returns:
The value of the nth term.
'''
if n<0:
raise ValueError("n cannot be negative")
return ((n/2) * (2 * a + (n - 1) * d))
def sum_gp(a: float, r: float, n: int) -> float:
'''
Returns the sum upto n terms of a Geometric Progression.
Parameters:
a: First term of Progression
r: Common ratio of the Progression
n: Value (int) upto which sum is to be calculated
Returns:
The value of the nth term
'''
if n<0:
raise ValueError("n cannot be negative")
total = float(0)
value = a
for i in range(n):
total = total + value
value = value * r
return total